Integration test

example

# spec/features/contacts_spec.rbrequire 'spec_helper'describe "Contacts" do
    describe "Manage contacts" do
        it "Adds a new contact and displays the results" do
            visit contacts_url
            expect{ click_link 'New Contact'
            fill_in 'Firstname', with: "John"
            fill_in 'Lastname', with: "Smith"
            fill_in 'mobile', with: "555-7888"
            click_button "Create Contact" }
                .to change(Contact,:count).by(1)
            page.should have_content "Contact was successfully created."
            within 'h1' do
                page.should have_content "John Smith"
            end
            page.should have_content "mobile 555-7888"
        end
    end
end

Another Example

require 'spec_helper'
describe "Contacts" do
    describe "Manage contacts" do
        ....
        it "Deletes a contact" do
            contact = Factory(:contact,
                firstname: "Aaron", lastname: "Sumner")
            visit contacts_path
            expect{
                within "#contact_#{contact.id}" do
                    click_link 'Destroy'
                end
            }.to change(Contact,:count).by(-1)
            page.should have_content "Listing contacts"
            page.should_not have_content "Aaron Sumner"
        end
    end
end




Capybara

Navigation

ONLY : GET Method
 visit('/projects')
 visit(post_comments_path(post))

 current_path.should == post_comments_path(post)

Links & Buttons


click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click_on('Link Text') # clicks on either links or buttons
click_on('Button Value') 

Interacting with forms


fill_in('First Name', :with => 'John')
fill_in('Password', :with => 'Seekrit')
fill_in('Description', :with => 'Really Long Text...')
choose('A Radio Button')
check('A Checkbox')
uncheck('A Checkbox')
attach_file('Image', '/path/to/image.jpg')
select('Option', :from => 'Select Box')
unselect 'March', :from => 'Month'

Matchers


page.has_selector?('table tr')
page.has_selector?(:xpath, '//table/tr')

page.has_xpath?('//table/tr')
page.has_css?('table tr.foo')
page.has_content?('foo')
has_button?
has_checked_field?
has_field?
has_link?
has_no_button?
has_no_checked_field?
has_no_table?

example


page.has_css?('p#foo') 
page.has_css?('p#foo', :count => 4) 
page.has_css?('li', :text => 'Horse', :visible => true)  

Matchers with Rspec



page.has_css?('table tr.foo')


vs


page.should have_css('table tr.foo')

finders


find_field('First Name').value
find_link('Hello').visible?
find_button('Send').click

find(:xpath, "//table/tr").click
find("#overlay").find("h1").click
all('a').each { |a| a[:href] }
find('#navigation').click_link('Home')
find('#navigation').should have_button('Sign out')

scope

within("li#employee") do
  fill_in 'Name', :with => 'Jimmy'
end

within(:xpath, "//li[@id='employee']") do
  fill_in 'Name', :with => 'Jimmy'
end


Java Script


it "Deletes a contact", js: true do
    DatabaseCleaner.clean
    contact = Factory(:contact, firstname: "Aaron", lastname: "Sumner")
    visit contacts_path
    expect{ 
        within "#contact_#{contact.id}" do
            click_link 'Destroy'
        end
        alert = page.driver.browser.switch_to.alert
        alert.accept
    }.to change(Contact,:count).by(-1)
    page.should have_content "Listing contacts"
    page.should_not have_content "Aaron Sumner"
end
DatabaseCleaner.clean
page.driver.browser.switch_to.alert




Rspec Advanced Topics








Stubs

Stubs

Stubbing a method means to override its implementation, providing a default implementation on which the calling code can rely.

  obj.stub(:message) { 'this is the value to return' }
  it "returns nil" do
    receiver.stub(:message)
    receiver.message.should be(nil)
  end




Mocks

Mocks

Mocking a method means to expect & test its implementation .


  obj.should_receive(:message)

Example

class Hello
  def say
    "hello world"
  end
end
describe Hello do
  context "saying hello" do 
    before(:each) do
      @hello = mock(Hello)
      @hello.stub!(:say).and_return("hello world")
    end

    it "#say should return hello world" do
      @hello.should_receive(:say).and_return("hello world")
      answer = @hello.say
      answer.should match("hello world")
    end
  end
end

Assignment




Watch Code School Videos :: Rspec





Good Luck !

Rspec Testing::2

By mohheader

Rspec Testing::2

  • 234