当我运行bundle exec rspec spec /我得到21个例子和3个失败。那些失败是:
故障:
1)用户has_password?如果密码匹配,则方法应为true 失败/错误:@ user.has_password?(@ attr [:password])。应该是be_true NoMethodError: '
中未定义的方法has_password?' for nil:NilClass
# ./spec/models/user_spec.rb:47:in
阻止(3个级别)
2)用户has_password?如果密码不匹配,则方法应为false 失败/错误:@ user.has_password?(“invalid”)。应该是__ false NoMethodError: '
中未定义的方法has_password?' for nil:NilClass
# ./spec/models/user_spec.rb:51:in
阻止(3个级别)
3)用户密码验证应接受有效的电子邮件地址 失败/错误:它“应该拒绝无效的电子邮件地址” NoMethodError: '
中未定义的方法it' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_3:0x00000102eb38b0>
# ./spec/models/user_spec.rb:97:in
阻止(3个级别)
我会发布我的user_spec.rb文件bc我认为它几乎是正确的,但并不完全。请注意已注释掉的结尾,我之前有过这些,但认为他们错了,所以评论出来。
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 valid attributes" do
User.create!(@attr)
end
describe "password encryption" do
before(:each) do
@user = User.create!(@attr)
end
it "should have an encrypted password attribute" do
@user.should respond_to(:encrypted_password)
end
it "should set the encrypted password" do
@user.encrypted_password.should_not be_blank
end
end
describe "has_password? method" do
it "should be true if the passwords match" do
@user.has_password?(@attr[:password]).should be_true
end
it "should be false if the passwords don't match" do
@user.has_password?("invalid").should be_false
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
short = "a" * 5
hash = @attr.merge(:password => short, :password_confirmation => short)
User.new(hash).should_not be_valid
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 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
# Put a user with given email address into the database.
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
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
end
end
end
end
我认为我的user.rb文件很好。
所以3次失败是我问题的一个方面,但真正让我担心的是以下命令:
捆绑exec rspec spec / models / user_spec.rb -e“has_password \?method”
终端的结果如下:
没有符合{:full_description =&gt; /(? - mix:has_password \\?\ method)/}的示例。
以0.00003秒结束 0个例子,0个失败
据Hartl说,我应该有2个例子,0个失败。想法?任何意见赞赏:)
答案 0 :(得分:0)
在你的user_spec.rb文件中。
确保您拥有:
before(:each) do
@user = User.create!(@attr)
end
紧跟在以下一行之后:
describe "has_password? method" do
教程中的代码中缺少它。你会看到它是密码加密块的一部分。看起来它会为用户进行测试。它不是很干......可能是为每个描述块运行该存根块的方法,但这比我更进一步。 :)希望它有帮助...让我的测试工作。