- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我一直在成功地遵循 Rails 教程 3,直到我到达第 7 章并实现了用户模型,现在我的 rspec 一直失败。
这是我的 user.rb 文件输出
class User < ActiveRecord::Base
attr_accessible :name, :email
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 6..40 }
before_save :encrypt_password
# Return tue if the user's password matches the submitted password.
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
def self.authenticate(email, submitted_password)
user = find_by_email(email)
return nil if user.nil?
return user if user.has_password?(submitted_password)
end
private
def encrypt_password
self.salt = make_salt unless has_password?(password)
self.encrypted_password = encrypt(password)
end
def encrypt(string)
secure_hash("#{salt}--#{string}")
end
def make_salt
secure_hash("#{Time.now.utc}--#{password}")
end
def secure_hash(string)
Digest::SHA2.hexdigest(string)
end
end
这是我的 users_spec.rb
require 'spec_helper'
describe User do
before(:each) do
@attr = {
:name => "Example User",
:email => "user@example.com",
:password => "foobar",
:password_confirmation => "foobar"
}
end
it "should create a new instance given a valid attribute" do
User.create!(@attr)
end
it "should require a name" do
no_name_user = User.new(@attr.merge(:name => ""))
no_name_user.should_not be_valid
end
it "should require an email address" do
no_email_user = User.new(@attr.merge(:email => ""))
no_email_user.should_not be_valid
end
it "should reject names that are too long" do
long_name = "a" * 51
long_name_user = User.new(@attr.merge(:name => long_name))
long_name_user.should_not be_valid
end
it "should accept valid email addresses" do
addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
addresses.each do |address|
valid_email_user = User.new(@attr.merge(:email => address))
valid_email_user.should be_valid
end
end
it "should reject invalid email addresses" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
addresses.each do |address|
invalid_email_user = User.new(@attr.merge(:email => address))
invalid_email_user.should_not be_valid
end
end
it "should reject duplicate email addresses" do
User.create!(@attr)
user_with_duplicate_email = User.new(@attr)
user_with_duplicate_email.should_not be_valid
end
it "should reject email addresses identical up to case" do
upcased_email = @attr[:email].upcase
User.create!(@attr.merge(:email => upcased_email))
user_with_duplicate_email = User.new(@attr)
user_with_duplicate_email.should_not be_valid
end
describe "passwords" do
before(:each) do
@user = User.create!(@attr)
end
it "should have a password attribute" do
@user.should respond_to(:password)
end
it "should have a password confirmation attribute" do
@user.should respond_to(:password_confirmation)
end
end
describe "password validations" do
it "should require a password" do
User.new(@attr.merge(:password => "", :password_confirmation => "")).
should_not be_valid
end
it "should require a matching password confirmation" do
User.new(@attr.merge(:password_confirmation => "invalid")).
should_not be_valid
end
it "should reject short passwords" do
short = "a" * 5
hash = @attr.merge(:password => short, :password_confirmation => short)
User.new(hash).should_not be_valid
end
it "should reject long passwords" do
long = "a" * 41
hash = @attr.merge(:password => long, :password_confirmation => long)
User.new(hash).should_not be_valid
end
end
end
最后是我的 rspec 的输出
Failures:
1) UsersController GET 'show' should be successfull
Failure/Error: @user = Factory(:user)
ArgumentError:
Factory not registered: user
# ./spec/controllers/users_controller_spec.rb:9:in `block (3 levels) in <top (required)>'
2) UsersController GET 'show' should find the right user
Failure/Error: @user = Factory(:user)
ArgumentError:
Factory not registered: user
# ./spec/controllers/users_controller_spec.rb:9:in `block (3 levels) in <top (required)>'
3) User should create a new instance given a valid attribute
Failure/Error: User.create!(@attr)
NoMethodError:
undefined method `password' for #<User:0x007f9d3684e0b0>
# ./spec/models/user_spec.rb:15:in `block (2 levels) in <top (required)>'
4) User should require a name
Failure/Error: no_name_user.should_not be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d36eacf38>
# ./spec/models/user_spec.rb:20:in `block (2 levels) in <top (required)>'
5) User should require an email address
Failure/Error: no_email_user.should_not be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d36e45978>
# ./spec/models/user_spec.rb:25:in `block (2 levels) in <top (required)>'
6) User should reject names that are too long
Failure/Error: long_name_user.should_not be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d36e0b2a0>
# ./spec/models/user_spec.rb:31:in `block (2 levels) in <top (required)>'
7) User should accept valid email addresses
Failure/Error: valid_email_user.should be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d36da4c80>
# ./spec/models/user_spec.rb:38:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:36:in `each'
# ./spec/models/user_spec.rb:36:in `block (2 levels) in <top (required)>'
8) User should reject invalid email addresses
Failure/Error: invalid_email_user.should_not be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d36d870b8>
# ./spec/models/user_spec.rb:46:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:44:in `each'
# ./spec/models/user_spec.rb:44:in `block (2 levels) in <top (required)>'
9) User should reject duplicate email addresses
Failure/Error: User.create!(@attr)
NoMethodError:
undefined method `password' for #<User:0x007f9d36c6c890>
# ./spec/models/user_spec.rb:51:in `block (2 levels) in <top (required)>'
10) User should reject email addresses identical up to case
Failure/Error: User.create!(@attr.merge(:email => upcased_email))
NoMethodError:
undefined method `password' for #<User:0x007f9d36c4d878>
# ./spec/models/user_spec.rb:58:in `block (2 levels) in <top (required)>'
11) User passwords should have a password attribute
Failure/Error: @user = User.create!(@attr)
NoMethodError:
undefined method `password' for #<User:0x007f9d36b3cda8>
# ./spec/models/user_spec.rb:66:in `block (3 levels) in <top (required)>'
12) User passwords should have a password confirmation attribute
Failure/Error: @user = User.create!(@attr)
NoMethodError:
undefined method `password' for #<User:0x007f9d369c27c0>
# ./spec/models/user_spec.rb:66:in `block (3 levels) in <top (required)>'
13) User password validations should require a password
Failure/Error: User.new(@attr.merge(:password => "", :password_confirmation => "")).
NoMethodError:
undefined method `password' for #<User:0x007f9d3699e5f0>
# ./spec/models/user_spec.rb:81:in `block (3 levels) in <top (required)>'
14) User password validations should require a matching password confirmation
Failure/Error: User.new(@attr.merge(:password_confirmation => "invalid")).
NoMethodError:
undefined method `password' for #<User:0x007f9d3698e600>
# ./spec/models/user_spec.rb:86:in `block (3 levels) in <top (required)>'
15) User password validations should reject short passwords
Failure/Error: User.new(hash).should_not be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d3697dda0>
# ./spec/models/user_spec.rb:93:in `block (3 levels) in <top (required)>'
16) User password validations should reject long passwords
Failure/Error: User.new(hash).should_not be_valid
NoMethodError:
undefined method `password' for #<User:0x007f9d3696c5a0>
# ./spec/models/user_spec.rb:99:in `block (3 levels) in <top (required)>'
Finished in 0.80301 seconds
35 examples, 16 failures, 2 pending
Failed examples:
rspec ./spec/controllers/users_controller_spec.rb:12 # UsersController GET 'show' should be successfull
rspec ./spec/controllers/users_controller_spec.rb:17 # UsersController GET 'show' should find the right user
rspec ./spec/models/user_spec.rb:14 # User should create a new instance given a valid attribute
rspec ./spec/models/user_spec.rb:18 # User should require a name
rspec ./spec/models/user_spec.rb:23 # User should require an email address
rspec ./spec/models/user_spec.rb:28 # User should reject names that are too long
rspec ./spec/models/user_spec.rb:34 # User should accept valid email addresses
rspec ./spec/models/user_spec.rb:42 # User should reject invalid email addresses
rspec ./spec/models/user_spec.rb:50 # User should reject duplicate email addresses
rspec ./spec/models/user_spec.rb:56 # User should reject email addresses identical up to case
rspec ./spec/models/user_spec.rb:69 # User passwords should have a password attribute
rspec ./spec/models/user_spec.rb:73 # User passwords should have a password confirmation attribute
rspec ./spec/models/user_spec.rb:80 # User password validations should require a password
rspec ./spec/models/user_spec.rb:85 # User password validations should require a matching password confirmation
rspec ./spec/models/user_spec.rb:90 # User password validations should reject short passwords
rspec ./spec/models/user_spec.rb:96 # User password validations should reject long passwords
对发生的事情有什么想法吗?我已经坚持了大约一个星期了
最佳答案
您的模型没有可访问的 password
或 password_confirmation
属性。变化:
attr_accessible :name, :email
到:
attr_accessible :name, :email, :password, :password_confirmation
关于ruby-on-rails - Rails 教程 3 第 7 章 : User Model Rspec Test Failing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9452841/
我使用 apt-get install libgtest-dev 安装了 gtest 我正在尝试检查它是否有效。 所以我在 eclipse 中编写了简单的测试代码。 但是有错误, undefined
($test) = (@test); $test = @test; 用一个括号括住变量,它访问数组的第一个元素。我找不到有关数组括号的信息。 最佳答案 ($test) = (@test); 这会将@t
在 clojure.test 中有一个允许同时测试多个设备的宏: are . 在 clojure.test 中,可以结合 are宏与 testing ? IE。就像是: (are [scenario
通常,Rust 中的单元测试被赋予一个单独的模块,该模块使用 #[cfg(test)] 进行条件编译: #[cfg(test)] mod tests { #[test] fn test
在过去,编程很少涉及猜测。我会写几行代码,一眼就能 100% 确定代码做什么和不做什么。错误主要是拼写错误,但与功能无关。 我相信在过去的几年中存在这种“试错”编程的趋势:编写代码(就像在草稿中一样)
在building the Kotlin compiler之后(在提交e80a01a处): ./gradlew dist 测试未成功通过: ./gradlew compiler:test 由于很少有测
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this qu
最近一直在思考模糊测试和猴子测试的区别。根据 wiki,猴子测试似乎“只是”一个单元测试,而模糊测试则不是。安卓有 UI/Application Exerciser monkey而且它看起来不像是单元
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
现在我正在使用 CMake 设置一个 C++ 测试环境。其实我已经意识到我想做什么,但我对两种不同的测试输出风格感到困惑。在我下面的示例中,“make test”实际上做了什么?我认为“make te
在 VS2012 中运行单个测试时,测试资源管理器底部会显示一个窗口,其中包括(假设失败)旁边带有“测试失败”的红色图标。紧随其后的是带有“已用时间”的失败消息。 我想简单地知道是否有办法清除这个窗口
bash 是否可以从 shell 执行命令,如果它返回某个值(或空值)则执行命令? if [ "echo test" == "test"]; then echo "echo test output
这个问题在这里已经有了答案: 8年前关闭。 Possible Duplicate: What is a smoke testing and what will it do for me? 为什么“冒烟
x86 下的并行编程可能很困难,尤其是在多核 CPU 下。假设我们有多核 x86 CPU 和更多不同的多线程通信组合。 单一作者和单一读者 单个读者多个作者 多个读者和单个作者 多个读者和多个作者 那
我使用Ctest来运行一堆使用add_test()注册的Google测试。当前,这些测试没有任何参数。但是,我想在运行--gtest_output=xml时为它们提供所有参数(所有参数都通用,特别是c
我有下表和数据: CREATE TABLE `test` ( `id` int(11) NOT NULL auto_increment, `name` varchar(8) NOT NULL,
go test 的两个标志 -parallel 和 -test.parallel 之间的区别以及哪个标志优先? -parallel n Allow parallel execu
在我的组件 AudioPlayer 中,我有一个 download() 方法: download() { this.audio.pause(); window.open(this.file,
您必须承认,对于 Rails 和数据库的新手来说,rubyonrails.org 上的官方解释使所有这四个任务听起来完全一样。引用: rake db:test:clone Recreate the
我过去曾讨论过这个话题,我想我可能知道答案,但我无法正确地表达出来。 这是我认为我所知道的: 如果您在编写测试之前已经有了关于事情如何工作的想法,那么我怀疑您是测试优先而不是测试驱动,因此您首先编写测
我是一名优秀的程序员,十分优秀!