Rspec 不会使这个简单的代码失败,即使它应该
控制器:
class FooController < ApplicationController
def create
end
end
控制器规范:
describe FooController
it "does bar" do
Foo.should_receive(:new).with("text" => "Lorem ipsum")
post :create, foo: { "text" => "Lorem ipsum" }
end
end
当我运行它时,rspec 说它成功了。但是,create
方法中永远不会调用 Foo.new
。但是,如果我将 post
函数调用中的 Lorem ipsum
更改为其他内容,则会失败。我希望这会失败,如果我将 Foo.new(params[:foo])
添加到 create
方法的主体中,就会成功。为什么情况并非如此?
Controller:
class FooController < ApplicationController
def create
end
end
Controller spec:
describe FooController
it "does bar" do
Foo.should_receive(:new).with("text" => "Lorem ipsum")
post :create, foo: { "text" => "Lorem ipsum" }
end
end
When I run this, rspec says that it's a success. However, Foo.new
is never called in the create
method. If I change the Lorem ipsum
in the post
function call to something else, however, it fails. I would expect this to fail, and succeed if I added Foo.new(params[:foo])
to the body of the create
method. Why is this not the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您在描述块上缺少“do”。尝试:
Looks like you are missing a "do" on the describe block. Try: