You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Example groups are executed in random orders so setting let must be taken care of
before(:all) does not rollback transaction! Test data created remains in the test database!
However, running rspec (on cmd line) will clear the test database. rake db:test:prepare will also clear the test database.
Testing samples:
Feature testing:
click_on"My Deals"expect(page).tohave_content"My Joined Deals"find("td[data-test='#{delivered_deal.id}']").shouldhave_button"Confirm Delivery"find("td[data-test='#{undelivered_deal.id}']").shouldhave_content"Pending"
context"Associations"doit{shouldbelong_to:category}endcontext"Validations"doit{shouldvalidate_presence_of:name}end# any other model methods
Helper testing:
describeDealsHelperdodescribe"#show_expiry_hours_in_words"doit"returns expirys in words with days and hours"doexpiry_hours={"24"=>"1 day","48"=>"2 days","49"=>"2 days 1 hour","50"=>"2 days 2 hours"}expiry_hours.eachdo |hours,words|
helper.show_expiry_hours_in_words(hours).should == wordsendendendend
Configuration
Add the below to ~/.rspec to format your rspec output in console
--color
--format documentation
Matching
# Controllerresponse.body.shouldhave_content"Success"response.body.shouldhave_css'#booking_ta_email[value="[email protected]"]'response.body.shouldhave_link"Go to My Bookings",href: bookings_path
Stub a METHOD and tells the testing to return our string
<model>.stub(:var){"#{Rails.root}/spec/fixture/stub_data.xml"}# by default, should_receive stubs the method and returns nil# use and_call_original to make the stub call the original methodHTTParty.should_receive(:post).at_least(3).and_call_original
Quickly create a test data object for testing
double(:asdasd,asking_price: 5)# returns an object with asking_price member
Testing HTTParty with retries
it"attempts url callback at least 3 times"doHTTParty.stub(:post).and_raise("Cannot connect")HTTParty.should_receive(:post).at_least(3)do_requestend
Testing Controller with Views
Rspec controller does not load the views so you will get an empty response.body
Add render_views to the controller spec to render views and get the response
require"spec_helper"describeWidgetsControllerdorender_viewsdescribe"index"doit"renders the index template"doget:indexresponse.shouldcontain("Listing widgets")endit"renders the widgets/index template"doget:indexresponse.shouldcontain("Listing widgets")endendend
HTTP request testing
Response of the request is found in response eg. response.body
Request can be debugged using request
Works for HTTParty
Easy pending
Add pending "Some message" will stop all execution in the example and mark it as pending. Useful if you need to stop the example from executing.
Testing before/after hooks in controller
Useful to test before_filter/after_filter hooks in controllers
require'spec_helper'describeApplicationControllerdodescribe'#set_user_id_for_geolocation_js'do# Create an anonymous controller which inherits from ApplicationControllercontrollerdodefindexrender:text=>"Hello"endendlet!(:user){create(:user)}it'sets the current user id in view if user is signed in'dosign_outuserget:indexassigns(:user_id).shouldbe_nilsign_inuserget:indexassigns(:user_id).should == user.idendendend