这是一个纯粹的句法问题。我对 RSpec 很陌生。
我基本上想在这个错误行的行上写一些东西:
controller.stub!(:current_user(:update_attributes => false))
有人知道如何正确写吗?
RSpec 的默认值如下所示:
User.stub(:find) { mock_user(:update_attributes => false) }
这是一个纯粹的句法问题。我对 RSpec 很陌生。
我基本上想在这个错误行的行上写一些东西:
controller.stub!(:current_user(:update_attributes => false))
有人知道如何正确写吗?
RSpec 的默认值如下所示:
User.stub(:find) { mock_user(:update_attributes => false) }
这看起来像一个案例stub_chain
:
controller.stub_chain(:current_user,:update_attributes).and_return(false)
请注意,这只是按照它们发生的顺序替换列表中的方法,因此为了有意义,您将current_user.update_attributes
在控制器中有一个。如果你有类似的东西@user.update_attributes
,我认为它不会起作用。
有关APIDock的更多信息
我只是盲目地玩了一千种变化,最后让它通过了:
controller.stub!(:current_user).and_return(@user)
@user.stub!(:update_attributes => false)
但说真的,这有任何意义吗?它正在过去:D
使用 RSpec 3.0 和rspec-mocks的方法是使用allow
.
before(:each) do
@user = mock_model(User)
allow(controller).to(receive(:current_user).and_return(@user))
allow(@user).to(receive(:update_attributes).and_return(false))
end
这并没有专门解决 update_attributes ,但以下内容对我有用。假设您的 current_user 助手位于 ApplicationController 中:
user = double(:user)
allow(controller).to(receive(:current_user).and_return(user))
allow(controller.current_user).to receive(:is_admin?).and_return(false)
current_user.is_admin? # => false