ruby-on-rails – Rails教程3第7章:用户模型Rspec测试失败

ruby-on-rails – Rails教程3第7章:用户模型Rspec测试失败,第1张

概述我一直在成功地遵循Rails教程3,直到我进入第7章并实现了用户模型,现在我的rspec一直在失败. 这是我的user.rb文件输出 class User < ActiveRecord::Baseattr_accessible :name, :emailemail_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/ivalidates :name, 我一直在成功地遵循Rails教程3,直到我进入第7章并实现了用户模型,现在我的rspec一直在失败.

这是我的user.rb文件输出

class User < ActiveRecord::Baseattr_accessible :name,:emailemail_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/ivalIDates :name,:presence => true,:length   => { :maximum => 50 }valIDates :email,:presence   => true,:format     => { :with => email_regex },:uniqueness => { :case_sensitive => false }valIDates :password,: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)    endprivatedef encrypt_password  self.salt = make_salt unless has_password?(password)  self.encrypted_password = encrypt(password)enddef encrypt(string)  secure_hash("#{salt}--#{string}")enddef make_salt  secure_hash("#{Time.Now.utc}--#{password}")enddef secure_hash(string)  Digest::SHA2.hexdigest(string)endend

这是我的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" dobefore(:each) do      @user = User.create!(@attr)    end    it "should have a password attribute" do      @user.should respond_to(:password)    endit "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  endend

最后这是我的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 seconds35 examples,16 failures,2 pendingFailed examples:rspec ./spec/controllers/users_controller_spec.rb:12 # UsersController GET 'show' should be successfullrspec ./spec/controllers/users_controller_spec.rb:17 # UsersController GET 'show' should find the right userrspec ./spec/models/user_spec.rb:14 # User should create a new instance given a valID attributerspec ./spec/models/user_spec.rb:18 # User should require a namerspec ./spec/models/user_spec.rb:23 # User should require an email addressrspec ./spec/models/user_spec.rb:28 # User should reject names that are too longrspec ./spec/models/user_spec.rb:34 # User should accept valID email addressesrspec ./spec/models/user_spec.rb:42 # User should reject invalID email addressesrspec ./spec/models/user_spec.rb:50 # User should reject duplicate email addressesrspec ./spec/models/user_spec.rb:56 # User should reject email addresses IDentical up to caserspec ./spec/models/user_spec.rb:69 # User passwords should have a password attributerspec ./spec/models/user_spec.rb:73 # User passwords should have a password confirmation attributerspec ./spec/models/user_spec.rb:80 # User password valIDations should require a passwordrspec ./spec/models/user_spec.rb:85 # User password valIDations should require a matching password confirmationrspec ./spec/models/user_spec.rb:90 # User password valIDations should reject short passwordsrspec ./spec/models/user_spec.rb:96 # User password valIDations should reject long passwords

关于发生了什么的任何想法?我已经坚持了一个星期了

解决方法 您的模型上没有可访问的密码或password_confirmation属性.更改:

attr_accessible :name,:email

至:

attr_accessible :name,:email,:password,:password_confirmation
总结

以上是内存溢出为你收集整理的ruby-on-rails – Rails教程3第7章:用户模型Rspec测试失败全部内容,希望文章能够帮你解决ruby-on-rails – Rails教程3第7章:用户模型Rspec测试失败所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1286697.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存