【问题标题】:Rspec: mock update_attributes in modelRspec:模型中的模拟 update_attributes
【发布时间】:2013-05-29 01:49:58
【问题描述】:

我是测试 Rails 应用程序的新手。现在,我正在学习如何模拟方法。

我有在我的用户模型中更改密码的方法:

  def change_password(user, pass)
    self.update_attributes(user) if self.password_hash == self.encrypt_password(pass)
  end

我要做的就是模拟方法 update_attributes。为此,我编写了一个测试:

let (:user) {FactoryGirl.build(:user)}
subject{user}
...
describe "#change_password" do
  before {user.save}
    context "when old password doesn't match" do
      it "should not update password" do
        user.should_not_receive(:update_attributes)
        user.change_password(user,"invalid")
      end
    end
  end

但是当我运行它时,我得到了错误:

NoMethodError:
       undefined method `should_not_receive' for #<User:0x0000000455daf8>

另外,我尝试像这样模拟 update_attributes 方法:

user.change_password.should_not_receive(:update_attributes)

我还有一个错误:

ArgumentError:
       wrong number of arguments (0 for 2)

而且我找不到任何关于如何在具有属性的方法中模拟方法的手册。

有人可以帮我解决我的问题吗?

【问题讨论】:

    标签: ruby-on-rails mocha.js stub


    【解决方案1】:

    要使用should_receive 方法,您需要设置config.mock_with :rspec,但我设置了config.mock_with :mocha。所以我应该使用expects 而不是should_receive

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多