require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe Suhyo::ViewMatchers do include Suhyo::ViewMatchers describe '#have_button' do it 'matches with no options' do ''.should have_button end it 'matches the text' do ''.should have_button(:text => 'Submit') end it 'does not match when there is no button' do ''.should_not have_button end it 'does not match when the text is wrong' do ''.should_not have_button(:text => 'Submit') end end describe '#have_link' do it 'matches with no options' do 'Foo'.should have_link end it 'matches the text' do 'Foo'.should have_link(:text => 'Foo') end it 'matches the URL' do 'Foo'.should have_link(:url => 'http://example.com') end it 'matches the ancestors' do '
Foo
'.should have_link(:ancestors => 'div#main') end it 'does not match when there is no link' do '

Foo

'.should_not have_link end it 'does not match when the text is wrong' do 'Foo'.should_not have_link(:text => 'Bar') end it 'does not match when the URL is wrong' do 'Foo'.should_not have_link(:url => 'http://google.com') end it 'does not match when the ancestors are wrong' do 'Foo'.should_not have_link(:ancestors => 'div#main') end end describe '#contain_in_order' do context 'matching before' do it 'matches when the content is in the right order' do '

Foo

Bar

'.should contain_in_order('Foo', :before => 'Bar') end it 'does not match when the content is in the reverse order' do '

Bar

Foo

'.should_not contain_in_order('Foo', :before => 'Bar') end it 'does not match when the first content is missing' do '

...

Bar

'.should_not contain_in_order('Foo', :before => 'Bar') end it 'does not match when the second content is missing' do '

Foo

...

'.should_not contain_in_order('Foo', :before => 'Bar') end it 'tells the expected order in the error message' do lambda do '

Bar

Foo

'.should contain_in_order('Foo', :before => 'Bar') end.should raise_error(Spec::Expectations::ExpectationNotMetError, /expected the following element's content to include "Foo" before "Bar":/) end it 'tells you if the reference content is missing' do lambda do '

Foo

...

'.should contain_in_order('Foo', :before => 'Bar') end.should raise_error(Spec::Expectations::ExpectationNotMetError, /expected the following element's content to include "Foo" before "Bar" but "Bar" was not found:/) end end context 'matching after' do it 'matches when the content is in the right order' do '

Foo

Bar

'.should contain_in_order('Bar', :after => 'Foo') end it 'does not match when the content is in the reverse order' do '

Bar

Foo

'.should_not contain_in_order('Bar', :after => 'Foo') end it 'does not match when the first content is missing' do '

Foo

...

'.should_not contain_in_order('Bar', :after => 'Foo') end it 'does not match when the second content is missing' do '

...

Bar

'.should_not contain_in_order('Bar', :after => 'Foo') end it 'tells the expected order in the error message' do lambda do '

Foo

Bar

'.should contain_in_order('Foo', :after => 'Bar') end.should raise_error(Spec::Expectations::ExpectationNotMetError, /expected the following element's content to include "Foo" after "Bar":/) end it 'tells you if the reference content is missing' do lambda do '

...

Foo

'.should contain_in_order('Foo', :after => 'Bar') end.should raise_error(Spec::Expectations::ExpectationNotMetError, /expected the following element's content to include "Foo" after "Bar" but "Bar" was not found:/) end end end describe '#have_restful_form' do def restful_form(method) case method when 'get' browser_method = 'get' hidden_field = nil when 'post' browser_method = 'post' hidden_field = nil when 'put' browser_method = 'post' hidden_field = '' when 'delete' browser_method = 'post' hidden_field = '' end %Q{
#{hidden_field}
} end %w(get post put delete).each do |expected_method| it "matches a #{expected_method.upcase} form" do restful_form(expected_method).should have_restful_form(expected_method) end (%w(get post put delete) - [expected_method]).each do |actual_method| it "does not match #{expected_method.upcase} when the method is #{actual_method.upcase}" do restful_form(actual_method).should_not have_restful_form(expected_method) end end end it 'matches the ancestors' do ('
' + restful_form('get') + '
').should have_restful_form('get', :ancestors => 'div#main') end it 'does not match if the ancestors are wrong' do restful_form('get').should_not have_restful_form('get', :ancestors => 'div#main') end it 'matches arbitrary attributes' do '
'.should have_restful_form('get', :action => 'http://example.com') '
'.should_not have_restful_form('get', :action => 'http://google.com') end it 'raises ArgumentError if method is not get, post, put, or delete' do lambda { restful_form('get').should have_restful_form('foo') }.should raise_error(ArgumentError) lambda { restful_form('get').should have_restful_form(42) }.should raise_error(ArgumentError) end it 'raises ArgumentError if options is not a hash' do lambda { restful_form('get').should have_restful_form('get', 42) }.should raise_error(ArgumentError) lambda { restful_form('get').should have_restful_form('get', 'foo') }.should raise_error(ArgumentError) end end end