开发者

testing the presence of form elements via rspec on ruby on rails

Hey all, I am having trouble testing the presence of form elements on my ruby on rails view. I am using rspec and have_selector to test.

Here is my test code:

it "should always have like button for root entity" do
  get :index
  @entities[0..3].each do |entity|
    response.should have_selector("form", 
      :method => "post", :action => "like/#{entity[:id]}") do |form|
        form.should have_selector("input", :value => "Like")
    end
  end
end

Here is my view's output for a particular entity:

    <th>
      <form method="post" action="/like/1"  class="button_to">
        <div>
          <input type="submit" value="Like" /><input name="authenticity_token" type="hidden" value="WEJPdFzphPFz+ld6BO8c/sMlhdfm+2Trp+3n0J8H5Cs=" />
        </div>
      </form>
    </th>

Here is my rspec error:

1) EntitiesController GET 'index' entities exist for non-signed in users should always have like button for root entity Failure/Error: form.should have_selector("input", :value => "Like") expected following output to contain a < input value='Like'/> tag.

If I comment out the line "form.should have_selector("input", :value => "Like")" so the test code is:

it "should always have like button for root entity" d开发者_如何学JAVAo
  get :index
  @entities[0..3].each do |entity|
    response.should have_selector("form", 
      :method => "post", :action => "like/#{entity[:id]}") do |form|
        #form.should have_selector("input", :value => "Like")
    end
  end
end

My rspec error is:

1) EntitiesController GET 'index' entities exist for non-signed in users should always have like button for root entity Failure/Error: response.should have_selector("form", expected following output to contain a < form method='post' action='like/2127'/> tag:

< form method="post" action="/like/2127" class="button_to">< div>< input type="submit" value="Like">

(only relevant part is c/ped)

I seem to be facing a lot of trouble with have_selector when it comes to form elements. Is there a better alternative or am I missing something obvious?

Thanks!


Your form's action is

action="/like/1"

but you're searching for

action="like/1"

i.e. you're missing a slash.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜