How to organise integration specs - ruby-on-rails

With rspec, it's quite clear how you should organise your unit specs. The directory structure inside spec is very similar to that found in the app directory, so model specs go in the model directory, controller specs go in the controller directory and so on.
But it's not so clear with integration testing. I have just one file pertaining to integration testing: spec/features/integration.rb
Is the idea to create one elaborate spec that tests every faculty of your application? Something like this:
require 'spec_helper'
describe "Everything", js: true do
before do
#user_0 = FactoryGirl.build(:user_0)
#user_1 = FactoryGirl.build(:user_1)
#user_2 = FactoryGirl.build(:user_2)
#user_3 = FactoryGirl.build(:user_3)
end
it "can create a user" do
visit root_path
click_link 'Sign In'
ap #user_0
fill_in('Email', with: #user_0.email)
fill_in('Password', with: #user_0.password)
click_button 'Sign in'
visit('/user_friendships')
end
it "can create a user" do
end
it "can create a user" do
end
it "can create a user" do
end
it "GET /root_path" do
visit root_path
page.should have_content("All of our statuses")
click_link "Post a New Status"
page.should have_content("New status")
fill_in "status_content", with: "Oh my god I am going insaaaaaaaaane!!!"
click_button "Create Status"
page.should have_content("Status was successfully created.")
click_link "Statuses"
page.should have_content("All of our statuses")
page.should have_content("Jimmy balooney")
page.should have_content("Oh my god I am going insaaaaaaaaane!!! ")
end
end
But a lot longer?
Should I use more than one file? How should I use the describe blocks? I'm only using one at the moment and that doesn't feel right.

The short answer is: No, it's not meant to go into one file.
Bigger projects split their acceptance test-suite into files based on feature sets. If you have a lot of tests, they are often split up into different directories. The way that you organize those tests is up to you. I have seen a lot of different approaches here. I tend to group spec with similar requirements on database setup, aka test-data.
If you want to have great guide for your rspec tests, go and have a look at this site: http://betterspecs.org/

It's usually desirable to split integration tests per page or flow in your application.
For example, you could try:
# spec/users/sign_in_spec.rb
RSpec.describe 'Users Sign In', js: true do
let!(:user) { FactoryGirl.build(:user_0) }
it "can sign in" do
visit root_path
click_link 'Sign In'
fill_in('Email', with: user.email)
fill_in('Password', with: user.password)
click_button 'Sign in'
page.should have_content('Signed In')
end
end
# spec/statuses/manage_status.rb
RSpec.describe 'Statuses', js: true do
it "should be able to post a status" do
visit root_path
page.should have_content("All of our statuses")
click_link "Post a New Status"
fill_in "status_content", with: "Everything is Alright"
click_button "Create Status"
page.should have_content("Status was successfully created.")
click_link "Statuses"
page.should have_content("All of our statuses")
page.should have_content("Everything is Alright ")
end
end
If you are looking for a way to organize your specs, you could use Capybara Test Helpers to encapsulate the code and make the tests more readable. For example:
# spec/users/sign_in_spec.rb
RSpec.describe 'Users Sign In', js: true, test_helpers: [:login] do
let!(:user) { FactoryGirl.build(:user_0) }
it "can sign in" do
visit root_path
login.enter_credentials(email: user.email, password: user.password)
current_page.should.have_content('Signed In')
end
end
# spec/statuses/manage_status.rb
RSpec.describe 'Statuses', js: true, test_helpers: [:statuses] do
it "should be able to post a status" do
visit root_path
current_page.should.have_content('All of our statuses')
statuses.post('Everything is Alright')
statuses.should_now.have_status('Everything is Alright')
end
end

Related

How to factor Capybara rspec testing code?

I need to test a system in which everything is available only after a user is signed in using Devise. Every time I use "it" I have to include the signup code.
Is there a way to factor the code below so that the "let's me make a new post" test and similar tests won't have to include the sign up?
describe "new post process" do
before :all do
#user = FactoryGirl.create(:user)
#post = FactoryGirl.create(:post)
end
it "signs me in" do
visit '/users/sign_in'
within(".new_user") do
fill_in 'Email', :with => 'user#example.com'
fill_in 'Password', :with => 'password'
end
click_button 'Log in'
expect(page).to have_content 'Signed in successfully'
end
it "let's me make a new post" do
visit '/users/sign_in'
within(".new_user") do
fill_in 'Email', :with => 'user#example.com'
fill_in 'Password', :with => 'password'
end
click_button 'Log in'
visit '/posts/new'
expect( find(:css, 'select#post_id').value ).to eq('1')
end
end
Your first option is to use the Warden methods provided, as per the documentation on this page:
https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara
Your second option is just to login for real in your tests as you have done in your examples. You can streamline this though by creating some helper methods to do the login work rather than duplicating the code in all of your tests.
To do this, I would create a support directory within your spec directory, and then a macros directory within that. Then create a file spec/support/macros/authentication_macros.rb:
module AuthenticationMacros
def login_as(user)
visit '/users/sign_in'
within('.new_user') do
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
end
click_button 'Log in'
end
end
Next, update your RSpec config to load your macros. In either spec_helper.rb or rails_helper.rb if you're using a newer setup:
# Load your support files
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# Include the functions defined in your modules so RSpec can access them
RSpec.configure do |config|
config.include(AuthenticationMacros)
end
Finally, update your tests to use your login_as function:
describe "new post process" do
before :each do
#user = FactoryGirl.create(:user)
#post = FactoryGirl.create(:post)
login_as #user
end
it "signs me in" do
expect(page).to have_content 'Signed in successfully'
end
it "let's me make a new post" do
expect( find(:css, 'select#post_id').value ).to eq('1')
end
end
Obviously, make sure you have password defined in your user factory.

Visiting More than one Page in a Describe Block Via Capybara

I'm writing some super simple integration specs, and while Capybara does simulate the browser/user interaction, I suspect that changing through several pages in one test causes issues. Mainly that the variable page doesn't get changed if you visit a few pages in a row. Here's what I mean:
require "rails_helper"
describe "The Initial Experience", :type => :feature do
it "User visits Narrowcontent" do
visit root_path
expect(page).to have_content "Sign In" && "Register"
click_on "Sign In"
expect(page).to have_content("Login Access")
within(".middle-login") do
fill_in "user_email", :with => ENV["admin_username"]
fill_in "user_password", :with => ENV["admin_pw"]
end
click_button 'Sign in'
save_and_open_page
#expect(page).to have_content("Dashboard")
end
end
I had to comment out the last expectation because it causes the test to fail. The keyword "Dashboard" DOES exist, but the problem is that after running click_button 'Sign In', the variable page still represents the old one, as if click_button 'Sign In'has no impact whatsoever.
Any tips?

Both my tests are failing even though they are in contrast (expect(...).to and expect(...).not_to)

I'm very new to rspec testing. I've tried the following test:
require 'spec_helper'
describe "CategoriesController" do
describe "#index" do
context "when signed in" do
it "should have the content 'Sign in'" do
visit categories_path
expect(page).to have_content('Sign in')
end
end
context "when signed in" do
it "should not have the content 'Sign in'" do
visit categories_path
expect(page).not_to have_content('Sign in')
end
end
end
end
Now, I will add some authentication but for not I just want one test to pass and the other to fail. At the moment both are failing, even though they are identical other than .to and .not_to
Any ideas what I'm doing wrong?
Your test looks like it ought to be in a Capybara feature spec, where the test imitates how a user interacts with a browser. But the describe "CategoriesController" do makes it look like you actually wrote a controller spec.
Try rewriting as such, after adding capybara to your Gemfile.
# in spec/features/sessions_spec.rb
require 'spec_helper'
feature "Sessions" do
scenario "when not signed in" do
visit categories_path
expect(page).to have_content('Sign in')
end
scenario "when signed in" do
visit categories_path
expect(page).not_to have_content('Sign in')
end
end
To debug the test once you have made it a feature spec, you can also add save_and_open_page like this:
scenario "when signed in" do
visit categories_path
save_and_open_page
expect(page).not_to have_content('Sign in')
end

Keeping user in the database between tests using Capybara?

My issue is that I have to create a new user and login for each individual capybara test.
An example is below:
require 'spec_helper'
describe "users" do
describe "user registration" do
it "should create a new user and log in" do
# Register a new user to be used during the testing process
visit signup_path
fill_in 'Email', with: 'testuser'
fill_in 'Password', with: 'testpass'
fill_in 'Password confirmation', with: 'testpass'
click_button 'Create User'
current_path.should == root_path
page.should have_content 'Thank you for signing up!'
end
end
describe "user login" do
it "should log in" do
# log in
visit login_path
fill_in 'Email', with: 'testuser'
fill_in 'Password', with: 'testpass'
click_button 'Log In'
current_path.should == root_path
page.should have_content 'Logged in!'
end
end
end
The login test fails because the user no longer exists in the database for that test.
This could be fixed simply by putting both in one test, but I believe that is bad practice.
Also I have another file which currently is registering and logging in between each test using a before_do, which also seems to be quite bad... you can see that code here.
For the record this is my first rails app so perhaps I am trying to do this the wrong way. I would like to dry it up as much as possible..
Is capybara really this bad to use on pages that require user login?
I have done it this way.
require "spec_helper"
describe "Users" do
subject { page }
describe "User Registration" do
before { visit signup_path }
let(:submit) { "Sign up" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar12"
fill_in "Password confirmation", with: "foobar12"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after registration" do
before { click_button submit }
it { should have_content 'Thank you for signing up!' }
end
describe "after registration signout and login" do
let(:user) { User.find_by_email('user#example.com') }
before do
click_button submit
visit signout_path
sign_in user # sign_in is a method which u can define in your spec/support/utilities.rb . Define once and use at multiple places.
end
it { should have_content 'Logged In!' }
it { should have_link('Logout') }
end
end
end
end
# spec/support/utilities.rb
def sign_in(user)
visit sign_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Log in"
end
your every describe and it block will run after the before block in parent that's why we need to click_button in every block in above test cases.

Failure/Error: expect { click_button submit }

I testing with rspec, Im still learning, I guess I'm on the right way... but when I test my rspec file I got this error:
Failures:
1) UsersController signup with valid information should create a user
Failure/Error: expect { click_button submit }.to change(User, :count).by(1)
count should have been changed by 1, but was changed by 0
# ./spec/controllers/user_controller_spec.rb:31
Finished in 1.16 seconds
2 examples, 1 failures
I know what this mean, but I don't know how to fix it, can anyone help me with this trouble please...Also I put my rspec file
require 'spec_helper'
describe UsersController do
describe "signup" do
before { visit new_user_registration_path }
let(:submit) { "Sign up" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Email", :with=> "user#example.com"
fill_in "Password", :with=> "foobar"
#fill_in "password_confirmation", :with=> "foobar"
end
(here is that the error appears...below line)
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
end
thanks for your attention
So, you have a failing spec. The spec itself looks ok. So I would check on
is the implementation in place? You can try out the scenario in your browser. Does it work there? If not, you have a failing test (which is good) and need to add the implementation.
if the implementation works, check the spec again carefully. Is the provided information
fill_in "Email", :with=> "user#example.com"
fill_in "Password", :with=> "foobar"
sufficient to create a user?
if you still haven't found the cause, you could use capybaras save_and_open_page (install launchy gem for that) and peek at the page during your test.
ADDITION:
ok, as I said, this should be an integration test - move it from the spec/controllers to the spec/requests folder (and do change the first line, as you're not describing the UsersController! but that should not lead to the problem).

Resources