In this article, we will study Include and Extend and see the differences between those two. Include is for adding methods to an instance of a class and extend is for adding methods to any type of object(Class or an instance of Class). Let’s take a look at a small example:
So far we’ve spent a lot of time going over controller and model testing, and use fabrication to generate test data. Now it’s time to put everything together for ingegration testing-in other words, makding sure thoes models and controllers all play nicely with other models and controllers in the application. These tests are called feature specs in RSpec.
A feature spec covers more ground, and represents how actual users will interact with your code. We write feature specs with Cypybara, an extremely useful Ruby library to help define steps of a feature spec and simulate real-world use of your application.
Add additional gems
add cypybara and launchy in the Gemfile
1 2 3 4 5
group :testdo gem "faker", "~> 1.1.2" gem "capybara", "~> 2.1.0" gem "launchy", "~> 2.2.0" end
and run $ bundle install
A basic feature spec
Capybara lets you simulate how a user would interact with your application throgh a web browser, using a series of easy-to-understand methods like click_link, fill_in, and visit. These methods let you to descirbe a test scenario for your app.
add a new file app/spec/features/user_sign_in_spec.rb
1 2 3 4 5 6 7 8 9 10 11 12 13
feature "User sign in"do scenario "with registered user"do user = Fabricate(:user) visit signin_path fill_in "Email", with: user.email fill_in "Password", with: user.password click_button "Sign in" expect(page).to have_content "Sign in successfully." expect(page).to have_content user.full_name end end