意外的 rspec 行为 Ruby on Rails Turorial 第 12 章

发布于 2024-10-23 20:17:56 字数 1526 浏览 1 评论 0原文

我看到一个仅取决于行位置的错误:

should change(Relationship, :count).by(-1)

例如,使用代码:

it "should destroy a relationship using Ajax" do
  lambda do
    xhr :delete, :destroy, :id => @relationship
    response.should be_success
  end.should change(Relationship, :count).by(-1) #<<-line is here

我得到 rspec 错误:

  1) RelationshipsController DELETE 'destroy' should destroy a relationship using Ajax
     Failure/Error: xhr :delete, :destroy, :id => @relationship
     ActionView::MissingTemplate:
       Missing template relationships/destroy with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:js, :html], :locale=>[:en, :en]} in view paths "#<RSpec::Rails::ViewRendering::PathSetDelegatorResolver:0x00000100a5b5f8>"
     # ./app/controllers/relationships_controller.rb:16:in `destroy'
     # ./spec/controllers/relationships_controller_spec.rb:44:in `block (4 levels) in <top (required)>'
     # ./spec/controllers/relationships_controller_spec.rb:43:in `block (3 levels) in <top (required)>'

但是使用代码:

it "should destroy a relationship using Ajax" do
  lambda do
    xhr :delete, :destroy, :id => @relationship
    response.should be_success
    should change(Relationship, :count).by(-1) #<<-Line moved to here
  end

...测试通过。

当我使用网络浏览器时,我看到了预期的行为。取消关注用户可以正确调整总数。

那么,这两个 rspec 测试不等效吗?我是否错误地确信第二次测试会通过?如果它们在功能上是等效的,为什么第一个会失败?

I am seeing an error that is only dependent on the location of the line:

should change(Relationship, :count).by(-1)

For example, with the code:

it "should destroy a relationship using Ajax" do
  lambda do
    xhr :delete, :destroy, :id => @relationship
    response.should be_success
  end.should change(Relationship, :count).by(-1) #<<-line is here

I get the rspec error:

  1) RelationshipsController DELETE 'destroy' should destroy a relationship using Ajax
     Failure/Error: xhr :delete, :destroy, :id => @relationship
     ActionView::MissingTemplate:
       Missing template relationships/destroy with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:js, :html], :locale=>[:en, :en]} in view paths "#<RSpec::Rails::ViewRendering::PathSetDelegatorResolver:0x00000100a5b5f8>"
     # ./app/controllers/relationships_controller.rb:16:in `destroy'
     # ./spec/controllers/relationships_controller_spec.rb:44:in `block (4 levels) in <top (required)>'
     # ./spec/controllers/relationships_controller_spec.rb:43:in `block (3 levels) in <top (required)>'

But with the code:

it "should destroy a relationship using Ajax" do
  lambda do
    xhr :delete, :destroy, :id => @relationship
    response.should be_success
    should change(Relationship, :count).by(-1) #<<-Line moved to here
  end

... the test passes.

I am seeing expected behavior when I use a web browser. Unfollowing a user adjusts totals correctly.

So, are these two rspec tests not equivalent? Am I falsely reassured that the second test passes? If they are functionally equivalent, why does the first one fail?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九局 2024-10-30 20:17:56

似乎这在一定程度上是由于

./app/controllers/relationships_controller.rb

./spec/controllers/relationships_controller_spec.rb

粘贴到错误文件中的代码(适当地)混淆而没有产生语法错误。修复我的错误导致通过 rspec 测试。

Seems this was due, in part, to having confused

./app/controllers/relationships_controller.rb

with

./spec/controllers/relationships_controller_spec.rb

Code pasted into the wrong file (appropriately) generated no syntax errors. Fixing my error lead to passing rspec tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文