Rspec fails to load support files - ruby-on-rails

I cannot get rspec to load support files, have tried:
config.include SessionHelper, :type => :controller
In the spec_helper file, Which gives me
NameError: uninitialized constant SessionsSpecHelper
Adding:
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Gives me:
NoMethodError: undefined method `join' for nil:NilClass
Adding:
require 'support/session_helpers.rb'
Provides no difference
support/session_helper.rb:
module SessionHelpers
def sign_up_with(email, password)
visit sign_up_path
fill_in 'Email', with: email
fill_in 'Password', with: password
click_button 'Sign up'
end
def sign_in
user = create(:user)
visit sign_in_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Sign in'
end
end

Have you noticed the error config.include SessionsHelper, :type => :controller
has Sessions
while your module module SessionHelpers has Session

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.

how signin via devise and capybara?

I am using Rspec, Capybara and Devise. I need to be able to sign in.
My test:
describe "POST #create" do
context "with valid params" do
it "creates a new Poll" do
#user = FactoryGirl.create(:user)
visit new_user_session_path
fill_in "user_email", :with => #user.email
fill_in "user_password", :with => "qwerty"
click_button "commitSignIn"
visit '/'
expect(page).to have_selector('.signin_username') # OK
binding.pry
end
end
end
In the Pry console, I tried to output current_user:
[1] pry(#<RSpec::ExampleGroups::PollsController::POSTCreate::WithValidParams>)> put current_user
NameError: undefined local variable or method `current_user' for #<RSpec::ExampleGroups::PollsController::POSTCreate::WithValidParams:0x00000008011c08>
from /home/kalinin/.rvm/gems/ruby-2.0.0-p598/gems/rspec-expectations-3.3.0/lib/rspec/matchers.rb:966:in `method_missing'
For further tests I need the current_user set.
Here's how I have included the Devise::TestHelpers:
# spec/spec_helper.rb
RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
config.include Devise::TestHelpers, type: :helper
config.include ApplicationHelper
end
I moved mine into a shared_context:
shared_context 'login' do
def log_in_with_user(user, options={})
email = options[:email] || user.email
password = options[:password] || user.password
# Do login with new, valid user account
visit new_user_session_path
fill_in "user_email", with: email
fill_in "user_password", with: password, exact: true
click_button "Log In"
end
end
Then in your test you can do:
describe "POST #create" do
include_context 'login'
let(:current_user) { FactoryGirl.create(:user) }
before do
log_in_with_user current_user
end
context "with valid params" do
it "creates a new Poll" do
visit '/'
expect(page).to have_selector('.signin_username') # OK
binding.pry
end
end
end

Undefined method when using rspec and macros

Here's my request spec:
require 'spec_helper'
describe "Password pages" do
describe "user views his passwords" do
let(:user) { create(:user) }
sign_in(:user)
end
end
spec_helper.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include Capybara::DSL
config.include LoginMacros
end
support/login_macros.rb
module LoginMacros
def sign_in(user)
visit new_user_session_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Sign in'
end
end
When running spec I get error:
/spec/requests/account_pages_spec.rb:9:in `block (2 levels) in <top (required)>': undefined method `sign_in' for #<Class:0x007fd4f97183f8> (NoMethodError)
I use this cool technics but another way:
Create page class:
class PageObject
include Capybara::DSL
def visit_page(page)
visit(page)
self
end
def login(user)
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Sign in'
end
end
create feature instead describe:
require 'spec_helper'
feature 'test authozire page' do
let(:login_page) { PageObject.new }
let(:user) { create(:user) }
scenario 'login page have field' do
page = login_page.visit_page(new_user_session_path)
expect(page.have_field?('email')).to be_true
expect(page.have_field?('password')).to be_true
end
scenario "user can login" do
login_page.visit_page(new_user_session_path).login(user)
# more awesome test
end
end

hot to write a test with capybara

This is my first time writing a test using RSpec with Capybara. Heres what I have so far:
require 'capybara/rspec'
describe "the signin process" do
before :each do
User.make(:email => 'test#test.com', :password => 'thisisatest')
end
it 'signs me in' do
visit 'sessions/new'
within("session") do
fill_in 'user email', :with => 'test#test.com'
fill_in 'password', :with => 'thisisatest'
end
click_link 'login'
expect(page).to have_content 'Thank You'
end
end
Here is the message i got when I ran the test:
the signin process
signs me in (FAILED - 1)
Failures:
1) the signin process signs me in
Failure/Error: User.make(:email => 'yedidyaweiner#gmail.com', :password => 'Shabbos!78')
NameError:
uninitialized constant User
# ./spec/features/sign_in_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.0006 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/features/sign_in_spec.rb:8 # the signin process signs me in
How can i fix this so the test passes? any suggestion on how to better write a test?
require 'spec_helper'
describe "The signin process" do
let!(:user) { User.create email: 'test#test.com', password: 'secret' }
it 'signs me in' do
visit 'sessions/new'
within("session") do
fill_in 'user email', with: user.email
fill_in 'password', with: user.password
end
click_link 'login'
expect(page).to have_content 'Thank You'
end
end

Using Capybara along with Devise?

I'm trying to write a Sign in integration test for my app + devise by using Capybara.
Here is what I have so far:
require 'spec_helper'
describe "the signup process", :type => :request do
before :each do
#user_1 = Factory.create(:user, :email => 'bob#golden.com', :password => 'iPassword')
end
it "signs me in" do
visit new_user_session_path
fill_in 'user[email]', :with => 'bob#golden.com'
fill_in 'user[password]', :with => 'iPassword'
click_link_or_button 'Sign In'
end
end
This is passing. Problem here is that it isn't checking that the user was signed in (cookie?) and that the url redirected correctly?
How can I add those details to this test? Also for an invalid login, how can I test to make sure the flash alert was set correctly?
Thank you
After click_link_or_button 'Sign In' add:
current_path.should == 'your path'
page.should have_content("Signed in successfully.")
/support/devise.rb
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end

Resources