Module: Test::Spec::Rails::ShouldSelect

Included in:
ShouldNotSelect
Defined in:
lib/test/spec/rails/should_select.rb

Instance Method Summary collapse

Instance Method Details

#select(selector, equality = true, message = nil, &block) ⇒ Object Also known as: contain, have

Wrapper for assert_select. Examples:

Test that the previous request has a login form:

page.should.select "form#login"

Test that a specific form has a field pre-filled (this is specific test/spec/rails):

 page.should.select "form#login" do |form|
   form.should.select "input[name=user_nick]", :text => @user.nick
 end

OR you can use contain
 page.should.contain "form#login"

 page.should.contain "form#login" do |form|
   form.should.contain "input[name=user_nick]", :text => @user.nick
 end

See the Rails API documentation for assert_select for more information



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/test/spec/rails/should_select.rb', line 20

def select(selector, equality=true, message=nil, &block)
  @@response_stack ||= []
  
  if @object.is_a?(Test::Unit::TestCase)
    @@response_stack.push(Test::Spec::Rails::DummyResponse.new(@object))
    
  elsif @object.is_a?(Array) && @object.first.is_a?(HTML::Tag)
    @@response_stack.push(Test::Spec::Rails::DummyResponse.new(
      @object.first.to_s, @@response_stack.last.headers
    ))
  else
    @@response_stack.push(Test::Spec::Rails::DummyResponse.new(@object.to_s,
      (@@response_stack.last.headers rescue 'Content-Type: text/html; charset=utf8')
    ))
  end
  
  @@response_stack.last.assert_select(selector, equality, message, &block)
  @@response_stack.pop
end