【问题标题】:How do I force a view failure in rspec?如何在 rspec 中强制查看失败?
【发布时间】:2011-05-01 11:46:00
【问题描述】:

我的 Rails 3 应用程序中有一个明显的错误。我正在将字段名称从“addr1”更改为“address”(在我的“代理”模型中)。所以很自然地,当我尝试打开编辑页面时,我会收到以下错误。

  undefined method `addr1' for #<Agency:0x00000100ebd0b0>

20 号线附近...

 18:   <div class="field">
 19:     <%= f.label :addr1 %><br />
 20:     <%= f.text_field :addr1 %>
 21:   </div>

很容易修复,但我想编写一个 rspec 测试来证明同样的错误(以防我重新引入它)。我已经尝试了许多测试。例如在我的 spec/views/agencies/edit.html.erb_spec.rb 我目前有...

 require 'spec_helper'
 describe "agencies/edit.html.erb" do
   it "renders the complete form" do
     assign(:agency, Factory(:agency, :name => "pat"))
     render # agencies/edit
     rendered.should match(/pat/)
     rendered.should match(/Editing agency/)
     assert_select "form", :action => agencies_path(@agency), :method => "post" do
       assert_select "input#agency_name", :name => "agency[name]"
       assert_select "input#agency_addr1", :name => "agency[name]"
     end
     puts rendered.to_s
   end
 end

但问题是它通过了!它不会触发我通过浏览器遇到的相同问题。而且,我故意留下了“addr1”的东西……当然,把它拿出来仍然是通行证。我希望第一次渲染会失败。 puts 显示...

<div class="field">
  <label for="agency_addr1">Addr1</label><br />
  <input id="agency_addr1" name="agency[addr1]" size="30" type="text" />
</div>

关于如何编写规范以显示页面真正正确呈现的任何想法?

谢谢

拍拍

PS。在探索这一点的过程中,我意识到 :name => "agency[name]" 在 assert_select 中没有任何作用。无论我放什么,测试都通过了。这是从生成的代码中复制的……对此有什么想法吗?

【问题讨论】:

  • 你是不是在出厂时把addr1改成address,在测试环境中运行迁移?
  • 你知道如何强制失败吗?我试着只写“失败”,但这没有用。好得令人难以置信。

标签: ruby-on-rails views rspec


【解决方案1】:

我敢打赌,这与未在测试数据库上运行的迁移有关。如果你跑

rake spec

迁移已为您运行 (http://www.ruby-forum.com/topic/170171)。如果你运行

rspec spec 

它们不会自动运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 2023-02-08
    • 2019-11-21
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多