如何强制 rspec 中的视图失败?
我的 Rails 3 应用程序中有一个明显的错误。我正在将字段名称从“addr1”更改为“地址”(在我的“代理”模型中)。因此,当我尝试拉出编辑页面时,很自然地会出现以下错误。
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”的东西……当然,把它拿出来仍然可以。我希望第一次渲染会失败。看跌期权显示...
<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 中不执行任何操作。无论我在那里放什么,测试都会通过。这是从生成的代码复制的......对此有什么想法吗?
I have an obvious error in my Rails 3 application. I was in the process of changing a field name from "addr1" to "address" (in my "agency" model). So naturally when I try to pull up the edit page I get the following error.
undefined method `addr1' for #<Agency:0x00000100ebd0b0>
around line 20...
18: <div class="field">
19: <%= f.label :addr1 %><br />
20: <%= f.text_field :addr1 %>
21: </div>
Easy to fix, but I want to write an rspec test that will demonstrate that same error (in case I ever re-introduce it). I have tried a number of tests. For example in my spec/views/agencies/edit.html.erb_spec.rb I currently have...
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
But the problem is that it passes! It does NOT trigger the same problem that I get through the browser. And, I left the "addr1" stuff in on purpose... taking it out still passes of course. I was hoping that the very first render would fail. The puts shows...
<div class="field">
<label for="agency_addr1">Addr1</label><br />
<input id="agency_addr1" name="agency[addr1]" size="30" type="text" />
</div>
Any ideas on how I can write an spec that shows me that a page really renders correctly?
thanks
pat
PS. In the process of exploring this I realized that the :name => "agency[name]" does nothing in the assert_select. The test passes no matter what I put there. And this is copied from generated code... any thoughts on that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我打赌这与未在测试数据库上运行的迁移有关。如果您运行
迁移,则会为您运行 (http://www.ruby-forum.com/topic /170171)。如果您运行
它们将不会自动运行。
I bet this is related to migrations not running on the test db. If you run
the migrations are run for you (http://www.ruby-forum.com/topic/170171). If you run
they will not be run automatically.