Using Rails 3.2 and the latest Rspec and Capybara, which means my Capybara specs live in spec/features.
I'm really new to Rails and testing, but I want to get used to testing. I ended up implementing OAuth before testing it. I finally got it working, and now I'm trying to retroactively test it (so I at least know if it breaks in the future). I'm trying to follow this tutorial, but things aren't working. Here's what I did:
1) Created spec/support/integration_spec_helper.rb with:
module IntegrationSpecHelper
def login_with_oauth(service = :google)
visit "/auth/#{service}"
end
end
2) Modified spec/spec_helper to include config.include IntegrationSpecHelper, :type => :request inside the Rspec.configure do block.
3) Created spec/features/omniauth_spec.rb with:
require 'spec_helper'
feature 'testing oauth' do
scenario 'should create a new tiger' do
login_with_oauth
visit new_tiger_path
fill_in 'tiger_name', :with => 'Charlie'
fill_in 'tiger_blood', :with => 'yes'
click_on 'Create Tiger'
page.should have_content("Thanks! You are a winner!")
end
end
Of course it's going to fail (I don't have tigers in my app) but I want it to fail on visit new_tiger_path. Instead, running the spec, I get:
1) testing oauth should create a new tiger
Failure/Error: login_with_oauth
NameError:
undefined local variable or method `login_with_oauth' for #<RSpec::Core::ExampleGroup::Nested_3:0x83355d8>
# ./spec/features/omniauth_spec.rb:4:in `block (2 levels) in <top (required)>'
So basically, it says there's no such thing login_with_oauth. This must be a really basic error, as my code isn't included for some reason.
I'm not using spork (trying to keep things simple).
Any idea what the problem might be? Thanks in advance!
If you are trying to use oauth from google, you'll want to change:
def login_with_oauth(service = :google)
to:
def login_with_oauth(service = :google_oauth2)
:google_oauth2 should also be the first argument to OmniAuth.config.add_mock, i.e.:
OmniAuth.config.add_mock(
:google_oauth2,
{
:info => {
:email => 'test#some_test_domain.com',
:name=>'Test User'
}
})
Don't forget to change:
config.include(IntegrationSpecHelper, :type => :request)
to:
config.include(IntegrationSpecHelper, :type => :feature)
inside the RSpec.configure block, as Christoph noted above.
A little late, but maybe I can help.
Got the same problem. It's caused by
config.include IntegrationSpecHelper, :type => :request
The paramater ':type' needs to be changed to ':feature' because you write a rspec feature test.
Solution:
config.include IntegrationSpecHelper, :type => :feature
Unfortunately this causes further problems, I couldn't solve yet.
Regards,
C-
Related
I'm attempting to set up system tests with Capybara and Selenium on an existing Rails 5.1 (Upgraded from Rails 4) app that already had capybara based feature tests. Here's what I've done so far.
In the gem file under group :development, :test:
gem 'chromedriver-helper'
gem 'selenium-webdriver'
gem 'rack_session_access'
In the environments/development.rb and environments/test.rb:
config.action_mailer.default_url_options = { host: 'localhost:3000' }
In the spec\rails_helper.rb:
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.configure do |config|
config.default_max_wait_time = 10 # seconds
config.default_driver = :selenium
config.app_host = 'http://localhost:3000'
config.server_host = 'localhost'
end
The issues I'm having are both with new systems tests and old feature tests.
With the system tests it appears that Capybara isn't creating a page object as I get undefined local variable or method 'page' Additionally when I duplicate the same test under the feature test directory I don't have this issue.
With the old Capybara feature tests, working with the rackTest driver, a Chrome window opens but I get No route matches [GET] "/rack_session/edit"
config.middleware.use RackSessionAccess::Middleware is already present in the environments/test.rb
Example system test:
require 'rails_helper'
describe User do
let(:user) { create :user }
let(:membership) { create :membership, admin: true}
let(:admin) { create :user, memberships: [membership] }
context 'viewing the index' do
it 'directs you to the appropriate page' do
set_current_user(admin)
visit root_url
click_button 'Manage Users'
expect(page.current_url).to end_with users_path
expect(page).to have_selector 'h1', text: 'Users'
end
end
end
Example feature test:
require 'rails_helper'
describe 'edit an assignment' do
let(:roster) { create :roster }
let(:user) { create :user, rosters: [roster] }
before :each do
Timecop.freeze Date.new(2018, 1, 10)
set_current_user(user)
end
after :each do
Timecop.return
end
context 'returns the user to the appropriate index page' do
let(:date_today) { Date.new(2017, 4, 4) }
let(:start_date) { Date.new(2017, 3, 31) }
let(:month_date) { date_today.beginning_of_month }
it 'redirects to the correct URL' do
visit roster_assignments_url(roster, date: date_today)
visit new_roster_assignment_url(roster, date: start_date)
click_button 'Create'
expect(current_url)
.to eq roster_assignments_url(roster,
date: month_date)
end
In the spec_helper:
def set_current_user(user)
page.set_rack_session user_id: user.id
end
You need to have the gem puma installed. New rails 5 projects have it installed by default, but your app was made in Rails 4, which is why it didn't.
Why is this? Well, if you were to do a bundle update (and I'll admit I can't explain why) you'd get this error when trying to run the specs, which is a lot more explanatory:
System test integration requires Rails >= 5.1 and has a hard dependency on a webserver and `capybara`, please add capybara to your Gemfile and configure a webserver (e.g. `Capybara.server = :webrick`) before attempting to use system tests.
Googling this error led me to this page, which explains Capybara needs a server.
After adding puma, I'm able to run system tests on your application.
A number of issues here.
Why are you setting - config.app_host = 'http://localhost:3000' ??? That would run the tests against your dev instance rather than the test instance Capybara starts. app_host really should only ever need to be set if you are doing subdomain based testing. This could be the reason for the no route error (normally rack_session_access would only be included in the test environment), or that could be caused by having not actually included the middleware as specified in the rack_session_access gem readme.
NEVER do expectations against current_path/current_url directly, instead use the provided matchers or you'll have flaky tests
expect(page).to have_current_path(users_path)
page is just an alias for Capybara.current_session and is a member of the module Capybara::DSL. If it's not available in the scope of your tests that it most likely means Capybara::DSL isn't included. That would normally be done by rspec-rails - https://github.com/rspec/rspec-rails/blob/master/lib/rspec/rails/vendor/capybara.rb#L21 - so it's possible you haven't actually set the test type to 'system'. If it's that it's not available in your spec_helper methods, just using Capybara.current_session instead is usually easier.
I have several non-javascript specs that use the ui to create and edit records.
When I run these specs the test database records are automatically removed for me by the rspec teardown for each test.
However the test below which is the first one to have :js => true for some ajax stuff isn't doing tear down of the records afterwards and then tests start to break because the database is no longer empty correctly when they start. The link and the group rows still exist in the test database.
# spec/features/verifying_link_spec.rb
require 'spec_helper'
describe "verification", :js => true, :type => :feature do
before :all do
User.create(:username => 'r#google.com', :password => 'esceptio')
end
before :each do
visit '/ladmin/login'
fill_in 'username', :with => 'r#google.com'
fill_in 'password', :with => 'esceptio'
find('input[value="Login"]').click
end
it "lets me verify a link" do
find('div#side div a', text: 'New Group').click
fill_in 'group[group_name]', with: 'Group Add'
click_button 'Save'
find('div#side div a', text: 'New Link').click
fill_in 'link[url_address]', with: 'http://www.a.com/newtest9876link'
fill_in 'link[alt_text]', with: 'abcd9876'
click_button 'Save'
this_year=Time.now.strftime('%Y')
l=Link.first
l.update_attribute(:verified_date, nil)
expect(Link.count).to eq 1
visit links_path
find('a', text: "verify")
click_link("verify", match: :first)
sleep(3)
expect(page).to have_content(this_year)
end
end
Right now I am using a workaround solution of using the ui to delete the records (below) but this should not be necessary
# added at bottom of spec
click_link('Details')
click_link('Delete')
page.driver.browser.switch_to.alert.accept
click_link('Groups')
click_link('Delete')
page.driver.browser.switch_to.alert.accept
None of my other unit tests or feature tests (except this one with js) have this problem. They all create records that get removed automatically.
I highly recommend using the database_cleaner gem to clean your database out between tests. Rspec wraps everything in a transaction and rolling it back once the example is finished. However, when you start using javascript you might be saving the data outside of the rspec transaction and then the database never reverts to its original state.
Truncation is slower than the transaction strategy. However you may only need the truncation strategy with JS tests. You could follow this guide to setup the database cleaner gem in that manner: http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/
If you find your tests taking eons, you might want to investigate some time looking at a gem that preloads your environment. I highly recommend zeus
I am setting up a basic template for having a capybara feature test in a rails application. I'm also using MiniTest instead of RSPEC.
Running Rake Test does not seem to be picking up my feature tests. I have one test in the file, running rake test does not change the number of assertions. Skipping the test does not show up either when I run rake test.
Here is a link to the repository: https://github.com/rrgayhart/rails_template
Here are the steps that I followed
I added this to the Gemfile and ran bundle
group :development, :test do
gem 'capybara'
gem 'capybara_minitest_spec'
gem 'launchy'
end
I added this to the test_helper
require 'capybara/rails'
I created a folder test/features
I created a file called drink_creation_test.rb
Here is the code from that feature test file
require 'test_helper'
class DrinkCreationTest < MiniTest::Unit::TestCase
def test_it_creates_an_drink_with_a_title_and_body
visit drinks_path
click_on 'new-drink'
fill_in 'name', :with => "PBR"
fill_in 'description', :with => "This is a great beer."
fill_in 'price', :with => 7.99
fill_in 'category_id', :with => 1
click_on 'save-drink'
within('#title') do
assert page.has_content?("PBR")
end
within('#description') do
assert page.has_content?("td", text: "This is a great beer")
end
end
end
I think I am having an issue with not connecting something correctly.
Please let me know if there is anything else I can provide which may help with diagnosing this issue.
Multiple things going on here. First, the default rake test task won't pick up tests not in the default test directories. So you need to either move the test file or add a new rake task to test files in test/features.
Since you are using capybara_minitest_spec you need to include Capybara::DSL and Capybara::RSpecMatchers into your test. And because you aren't using ActiveSupport::TestCase or one of the other Rails test classes in this test, you may see inconsistencies in the database because this test is executing outside of the standard rails test transactions.
require 'test_helper'
class DrinkCreationTest < MiniTest::Unit::TestCase
include Capybara::DSL
include Capybara::RSpecMatchers
def test_it_creates_an_drink_with_a_title_and_body
visit drinks_path
click_on 'new-drink'
fill_in 'name', :with => "PBR"
fill_in 'description', :with => "This is a great beer."
fill_in 'price', :with => 7.99
fill_in 'category_id', :with => 1
click_on 'save-drink'
within('#title') do
assert page.has_content?("PBR")
end
within('#description') do
assert page.has_content?("td", text: "This is a great beer")
end
end
end
Or, you could use minitest-rails and minitest-rails-capybara to generate and run these tests.
$ rails generate mini_test:feature DrinkCreation
$ rake minitest:features
I believe minitest has it's own gem for rails when using capybara: minitest-rails-capybara
Following the instructions over there might help, but I've never set up capybara with mini test before.
I have all my capybara tests working with my authlogic members area using the default driver, but when i change one test to use selenium driver as it has ajax in it, it gives my theis error :
You must activate the Authlogic::Session::Base.controller with a controller object before creating objects
Things are working with default driver for authlogic so must be something to do with selenium ??
I have include Authlogic::TestCase in my spec_helper and
activate_authlogic
domain.user_sessions.create(user)
in a before each.
Any one help me with this please ?
thanks rick
I posted a cucumber solution here: Log-in through authlogic without having to fill in form every time
For RSpec integration tests it's similar.
In your spec_helper.rb:
require "authlogic/test_case"
RSpec.configure do |config|
...
config.include Authlogic::TestCase
ApplicationController.skip_before_filter :activate_authlogic
config.before(:each, :type => :request) do
activate_authlogic
UserSession.create(User.find_by_email!(email))
end
...
end
Obviously, if your site is not login only you may want to move the two lines in config.before into a before block in your specific test for logged in specs. If you leave as is you can delete the session with UserSession.find.destroy or obviously follow the logout link (if this makes more sense in your spec).
I think the following code will work to activate authlogic:
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
Having said that, I prefer defining a step that actually goes to the login form, fills it out, and logs in. It's slower, but I rarely run my entire integration test suite manually, usually the continuous integration server takes care of that.
This work for me (Rails 3.2.1) :
In spec_helper.rb
require 'authlogic/test_case'
include Authlogic::TestCase
In In my controller_specs :
def valid_session
activate_authlogic # run before tests are executed
user = Factory(:user)
UserSession.create(user, true) #create an authlogic session
#user = #controller.current_user
{}
end
# exemple of valid_session utilization in your test:
# valid_session
# user_id = #user.id
#
# or
#
# get :index, {}, valid_session
Enjoy!
I am trying to call a method in my Rails 3 code but am getting:
Failure/Error: integration_sign_in wrong_user
ArgumentError:
wrong number of arguments (0 for 1)
Here is the calling code (in an RSpec helper):
before(:each) do
wrong_user = Factory(:user, :email => "test#test.com", :password=>"hellohello", :password_confirmation => "hellohello")
integration_sign_in wrong_user
end
So it is clearly passing one argument. If the argument is null for some reason, would that make it consider it not a parameter?
RELEVANT BACKSTORY: For testing, I just switched from webrat to capybara. As suggested in Railscast 257, I also installed the launchy and database_cleaner gems. When I was using webrat, the code above worked as expected, but now (I believe related to database_cleaner) something is going wrong.
Possibly relevant: In my spec_helper.rb I changed to:
config.use_transactional_fixtures = false (even though 'true' has the same problem)
Any ideas? Thanks.
It looks like the argument error was not actually in this function (even though the top of the stack makes it look like it is. The actual error is inside of the integration_sign_in function:
def integration_sign_in(user)
visit signin_path
fill_in :email, :with => user.email
fill_in :password, :with => user.password
click_button
end
It looks like click_button needs an argument in capybara, but it does not in webrat.
integration_sign_in(wrong_user)