没有方法错误 - 从rubystutorial.org保存

时间:2014-06-18 03:10:39

标签: ruby-on-rails ruby rspec save nomethoderror

我一直在rubystutorial.org上关注Hartl的书并坚持这个错误

当我运行rspec测试时,它告诉我用户

不存在方法(保存)

这是我的代码和测试。我找不到它的错误以及@ user

中不存在该方法的原因

user_spec.rb

require 'spec_helper'

describe User do

  before do
    @user = User.new(name: "Example User", email: "user@example.com") 
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }

  it { should be_valid }

  describe "when name is not present" do
    before { @user.name = " " }
    it { should_not be_valid }
  end

  describe "when email is not present" do
    before { @user.email = " " }
    it { should_not be_valid }
  end

  describe "when name is too long" do
    before { @user.name = "a" * 51 }
    it { should_not be_valid } 
  end

  describe "when email format is invalid" do
    it "shoud be invalid" do 
      addresses = %w[user@foo,com user_at_foo.org example.user@foo. foo@bar_baz.com foo@bar+baz.com]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        expect(@user).not_to be_valid
      end
    end
  end

  describe "when email format is valid" do 
    it "should be valid" do 
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      addresses.each do |valid_address|
        @user.email = valid_address
        expect(@user).to be_valid
      end
    end
  end

  describe "when email address is already taken" do 
    before do
      user_with_same_email = @user.dup
      user_with_same_email = @user.email.upcase
      user_with_same_email.save
    end

    it { should_not be_valid }
  end

end

我的user.rb

class User < ActiveRecord::Base
    before_save { self.email = email.downcase }
    validates(:name, presence: true, length: { maximum: 50 } )
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates(:email, presence: true, format: { with: VALID_EMAIL_REGEX },
                      uniqueness: { case_sensitive: false } )
end

确切的错误是

Failures:

1) User when email address is already taken 

 Failure/Error: user_with_same_email.save
 NoMethodError:
   undefined method `save' for "USER@EXAMPLE.COM":String
 # ./spec/models/user_spec.rb:55:in `block (3 levels) in <top (required)>'

Finished in 0.411 seconds
24 examples, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:58 # User when email address is already take

2 个答案:

答案 0 :(得分:7)

您正在定义一个用户,然后在下一行中更改字符串(电子邮件,因此电子邮件没有保存方法):

user_with_same_email = @user.dup
user_with_same_email = @user.email.upcase
user_with_same_email.save

你应该为此改变它:

user_with_same_email = @user.dup
user_with_same_email.email = @user.email.upcase
user_with_same_email.save

答案 1 :(得分:2)

<强>对象

这里的问题可以通过错误本身看出来:

  

未定义方法`save'为“USER@EXAMPLE.COM”:字符串

您遇到的问题是 .save不可用,但是您尝试调用它的对象无法处理它。

问题特别与这一行有关:

  

user_with_same_email = @ user.email.upcase

-

<强>修正

问题是您将user_with_same_email变量设置为@user.email.upcase。您可以执行zishe建议的内容(设置新变量的email属性)

我真的不明白您要实现的目标 - 因为您的User模型会在downcase回调中将电子邮件设置为before_save