Rails Testing and Debugging Flashcards
spec directory
rails file structure to store tests
typically has a separate models and features sub-directory
Why separate spec/features and spec/models?
Allows you to take advantage of RSpec’s built in assumptions (all tests in features are feature test, etc.)
Without this, you need to declare a type for each test: “RSpec.describe ‘this is a feature test’, type: :feature do”
Capybara method that allows us to use CSS Selectors to select elements and then write expectations just for that area of the page
within
e.g.,
within(‘#artist-13’) do
expect(page).to have_content(‘content about this artist’)
end
Spec File naming convention
test files MUST end with _spec.rb
convention is to name spec files in model folder after the model its testing.
no convention for feature testing
Spec File naming convention
test files MUST end with _spec.rb
convention is to name spec files in model folder after the model its testing.
no convention for feature testing. You could match your views, or match the headline of each user story
Spec File naming convention
test files MUST end with _spec.rb
convention is to name spec files in model folder after the model its testing.
no convention for feature testing. You could match your views, or match the headline of each user story
before :each
block that will run before every test (every it block), used to create setup for many test and DRY up test files (similar to setup method in Minitest)
method to check that some content appears anywhere on a page
have_content
method to check if there is a button with a particular label
have_button
method to check if there is a specific link
have_link
method to check if there is a particular css selector (often used to verify images)
have_ccs
method to check if there is a particular css selector (often used to verify images)
have_ccs
method to check if there is a link or button with a matching label
click_on(‘label’)
or
click_button
click_link
Capybara method to return a single element that matches a css selector, a
find
song_14 = page.find(‘#song-14’)
expect(song_14).to have_content(‘Raspberry Beret’)
Capybara method to return an array of all elements that match a css selector
all
all_songs = page.all(‘.song’)
expect(all_songs[2]).to have_content(‘Purple Rain’)
How to test for sorted elements
Use all method, ccs selectors, and array methods to verify a specific order.
within ‘#best-users’ do
expect(page.all(‘.user’)[0]).to have_content(“megan”)
expect(page.all(‘.user’)[1]).to have_content(“brian”)
expect(page.all(‘.user’)[2]).to have_content(“sal”)
end