开发者

How do I force a view failure in rspec?

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]" siz开发者_开发技巧e="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?


I bet this is related to migrations not running on the test db. If you run

rake spec

the migrations are run for you (http://www.ruby-forum.com/topic/170171). If you run

rspec spec 

they will not be run automatically.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜