Gordon
2011-Nov-18 11:18 UTC
[rspec-users] What does :count actually mean in assert_select?
Hi guys, I tried reading up the RSPEC Book (Dec 2010) and googled a bit but I could not find anything. I''m using Rspec2. Example: spec/factories/categories.rb ===================== FactoryGirl.define do factory :category_intakes, :class => ''category'' do name ''intakes and filters'' description ''airfilters and etc.'' created_by 1 updated_by 1 end factory :category_audio, :class => ''category'' do name ''audio'' description ''in car entertainment'' created_by 1 updated_by 1 end end spec/views/categories/index.html.erb ============================ require ''spec_helper'' describe "categories/index.html.erb" do before(:each) do assign( :categories, [ FactoryGirl.create(:category_intakes), FactoryGirl.create(:category_audio), ] ) end it "renders a list of categories" do render assert_select "tr>td", :text => "intakes and filters".to_s, :count => 1 assert_select "tr>td", :text => "audio".to_s, :count => 1 end end I noticed that the index view spec above passes but when I change the count to 2, it fails saying that only 1 element is expected. Question: 1) what does :count actually mean? Does it mean the total number of fixture objects expected? 2) does the ordering of the rendered fixture items matter ( ie. :category_audio is expected before :category_intakes)?
Gordon Yeong
2011-Nov-18 11:29 UTC
[rspec-users] What does :count actually mean in assert_select?
I think i answered my own question. :count refers to the number of fixture objects. The reason why :count => 2 was passing for views index specs is because 2 objects are being mocked and they are identical. Hence, there are 2 stock tests: 1) asserts that there are 2 <td> elements which have the name of ''Name'' 2) asserts that there are 2 <td> elements which have the name of ''Description'' This is what a stock view index spec looks like: ---- Stock index spec view ---- describe "makes/index.html.erb" do before(:each) do assign(:makes, [ stub_model(Make, :name => "Name", :description => "Description", :created_by => "", :updated_by => "" ), stub_model(Make, :name => "Name", :description => "Description", :created_by => "", :updated_by => "" ) ]) end it "renders a list of makes" do render # Run the generator again with the --webrat flag if you want to use webrat matchers assert_select "tr>td", :text => "Name".to_s, :count => 2 # Run the generator again with the --webrat flag if you want to use webrat matchers assert_select "tr>td", :text => "Description".to_s, :count => 2 end end ---- Stock index spec view ---- -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20111118/6686fe8f/attachment.html>