gpt4 book ai didi

ruby-on-rails - Mocha 对关联构建调用失败的期望

转载 作者:行者123 更新时间:2023-12-04 06:31:03 25 4
gpt4 key购买 nike

我有这个例子:

# GET New
context "on get to new" do
it "should assign cardset" do
@profile.cardsets.expects(:build).once.returns(Factory.stub(:cardset))
get :new
assigns[:cardset].should_not be_nil
end
end

要测试此方法:
# GET /cardsets/new
def new
@cardset = current_user.cardsets.build
end

我试图强制该关联是从 current_user 建立的。确保用户只创建属于自己的东西。我正在使用非常相似的期望来确保他们正在调用 find来自 current_user对象,它可以找到,但是在运行上面的示例时,我得到:
6)
Mocha::ExpectationError in 'CardsetsController for a logged in user on get to new should assign cardset'
not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked: [#<Cardset:0x102eaa8c8>, #<Cardset:0x102e12438>].build(any_parameters)
satisfied expectations:
- allowed any number of times, not yet invoked: ApplicationController.require_user(any_parameters)
- allowed any number of times, already invoked twice: #<CardsetsController:0x1030849c8>.current_user(any_parameters)

/Applications/MAMP/htdocs/my_app/spec/controllers/cardsets_controller_spec.rb:32:

最佳答案

在对从 current_user 返回它的函数 stub 后,您将期望添加到 @profile。可能你需要做的是:

# GET New
context "on get to new" do
it "should assign cardset" do
@profile.cardsets.expects(:build).once.returns(Factory.stub(:cardset))
controller.stubs(:current_user).returns(@profile)
get :new
assigns[:cardset].should_not be_nil
end
end

关于ruby-on-rails - Mocha 对关联构建调用失败的期望,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3230881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com