I have been writing test for a old project using Cucumber. When I run the test sweet using the Cucumber command there is no problem but when I do cucumber features/users/signup.feature I get a message to implement the steps definitions.
You can implement step definitions for undefined steps with these snippets:
any ideas on what I am missing.
At this point this is not a problem but when I have more test I know this is going to be a major problem.
# features/users/login.feature
#javascript
Feature: Users should be able to log in
Scenario: User should be able to log in
Given I am signed in as a user
Then I should see 'Signed in successfully.'
#features/step_definitions/session_steps.rb
Given 'I am signed in as a user' do
#user = Fabricate(:user)
visit new_user_session_path
fill_in 'user_email', with: #user.email
fill_in 'user_password', with: #user.password
click_button 'Sign In'
end
try this:
cucumber features/users/signup.feature -r features/
Cucumber knows about a particular data structure, if you use your own data structure, you need to rely on tagging or recursively stating where your dependency files live
Related
I am trying to test what happens after logging in through Devise gem. For example, I have the controller to go to student_dashboard_path after users successfully login.
How can I test this with Capybara and Rspec?
I currently have this in:
/spec/features/user_signs_in_sees_dashboard_spec.rb
require 'rails_helper'
feature 'User sign in' do
scenario 'successfully from sign in page and sees student dashboard' do
sign_in
visit student_dashboard_path
expect(current_path).to eq(student_dashboard_path)
end
end
and I have this in:
/spec/support/features/sign_in.rb
module Features
def sign_in
visit user_session_path
fill_in 'Email', with: User.first.email
fill_in 'Password', with: User.first.password
click_button 'Log in'
end
end
and I am getting this error message:
1) User signs in successfully from sign in and sees dashboard
Failure/Error: expect(current_path).to eq(student_dashboard_path)
expected: "/student/dashboard"
got: "/student/login"
I am not sure why I am not able to log in and see the student dashboard.
I'm leaving my original answer below for anyone still on Capybara < 2.5.0 but in 2.5.0 you can now do
expect(page).to have_current_path(<expected path>)
and it will use Capybara's waiting behavior while checking for the path
---- Below is only for Capybara version < 2.5.0
expect(current_path).to eq(...) doesn't wait for the path to change, it just compares to the current path at the time it's called. If you put a sleep after the click button I bet it works. A better solution would be to have something like
expect(page).to have_text('You are now logged in')
after the click_button. That would cause capybara to wait until the log in has completed, the page loads (and the logged in notice appears), and therefore until the current_path has changed too.
Use capybara save_and_open_page in middle to figure out if the fields are properly set. If you work on the same machine you can switch to selenium to test out on the real browser.
Also be sure that this code does not need any JS to work because default capybara matchers will not be able to run it.
I'm trying to test devise sign in, sign out and all the other scenarios, however I cannot get a single scenario to past, lets take login failure
in my feature I have
scenario 'user cannot sign in if not registered' do
login_as('user2#example.com', 'meow')
visit overviews_path
save_and_open_page
expect(page).to have_content I18n.t 'devise.failure.not_found_in_database', authentication_keys: 'email'
end
I also have the sign_in helper setup as;
def sign_in(email, password)
user.confirm!
visit new_user_session_path
fill_in 'Email', with: email
fill_in 'password', with: password
click_button 'Log in'
end
however this create an error;
expected to find text "Invalid email or password." in "TypeError at /overviews ======================= > no implicit conversion of Symbol into Integer spec/features/users/sign_in_spec.rb, line 14
any ideas?
You named your helper method sign_in but you're calling login_as in your scenario. You should use one approach or the other, not both.
UPDATE: OK, rechecked the documentation, and you should either use your own helper so that you're emulating an actual user signing in, or the login_as provided by Warden, in which case make sure you're included this in your tests / rspec setup:
include Warden::Test::Helpers
Warden.test_mode!
On a side note, you should confirm your user in your factory/fixture, not in your helper method (where it's probably not defined).
I'm developing a sign in/out function on a web app that i'm building for learning.
During the week, i wasn't able to pass the following rspec test:
describe "sucessfull log in" do
let(:user) {FactoryGirl.create(:user)}
before do
visit signin_path
fill_in "Email", with: user.email.upcase
fill_in "Password", with: user.password
click_button 'Sign in'
end
it {should have_title(user.name)}
it {should have_link('Account')}
it {should have_link('Profile', href: user_path(user))}
it {should have_link('Sign out', href: signout_path)}
end
Then yesterday and end up re writing this code again, but forgot to add that .upcase function. I Left just .upcase.email. Suddenly, the test passed.
Ok, i'm a newbie developer... so what happaned? Why the upcase function did not let rspec log in successfully?
thanks in advance!
Why are you upcasing the email? Would you be equally surprised if .reverse made it fail? You're changing the email address.
EXAMPLE#EMAIL.COM is not the same as example#email.com, strictly speaking. It might be equivalent for your application, but you need to make the application know that.
Are you using devise? If so, the config/devise.rb file will have:
# Configure which authentication keys should be case-insensitive.
# These keys will be downcased upon creating or modifying a user and when used
# to authenticate or find a user. Default is :email.
config.case_insensitive_keys = [ :email ]
Which should make what you're doing work. If you're not using it, whatever framework you're using will probably have a similar option, and if you're doing it yourself, you'll need to handle it yourself.
I cant find solution for my problem: cant login user in rspec test.
I tried this code, following by this link - https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs
require 'spec_helper'
require 'factory_girl_rails'
describe "ololo" do
it "blocks unauthenticated access" do
user = User.create(email: "lol#mail.com", password: "123456")
request.env['warden'].stub(:authenticate!).
and_throw(:warden, {:scope => :user})
visit "/tasks"
page.should have_content("Signed in successfully.")
end
end
/error/
Failure/Error: page.should have_content("Signed in successfully.")
expected there to be text "Signed in successfully." in "Task App Home Login You need to sign in or sign up before continuing. Sign in Email Password Remember me Sign upForgot
your password? Task App by Denys Medynskyi"
also tried this link - http://codingdaily.wordpress.com/2011/09/13/rails-devise-and-rspec/.
Devise wiki tutorial is also not working for me.
Please, I'm stuck hardly. Help me anyone.
Without seeing your error message, I guest one possible reason is you created hard record of User to test db. You may have run this test once, so in second time this creation fails because record with same email exists.
You've required FactoryGirl. Why don't you use FactoryGirl to create the fixture data?
In spec/factories/user.rb
FactoryGirl.define do
factory :user do
email: "lol#mail.com"
password "123"
end
end
In your test, write
FactoryGirl.create(:user)
Add
Comparing with the tut you lack two steps:
You need to use FactoryGirl to replace the hard created record, as writing above.
You need to execute the sign_in step before visit the private page.
Maybe you would be good with #2 only if you cleared the db after tests. Anyway factory data is recommended over hard data.
I am working on tests but running in to a road block on pages that require a current_user. I am using minitest, capybara, factorygirl, and authlogic, in rails 3.2.9 with ruby 1.9.3p327. I installed minitest as a separate gem, and seem to have the test environment working correctly.
I have a factory that creates a valid user...I call that factory from in a test like this:
describe "UsersAcceptanceTest" do
it "must load and include content" do
FactoryGirl.create(:user)
visit users_path
page.must_have_content("cPanel")
end
end
The FAIL is correct in informing me that the content "cPanel" was not found (cPanel is a link available to logged in users). The fail error goes on to alert me that it was not found in "log in, forgot password, contact" ... which of course means that the test routed correctly to users_path, but was redirected by authlogic because the user is not logged in. Users cant create themselves in my system and therefor are not auto-logged in on create.
How to I also get the factory to create a new user session with the newly created user?
You can do it this way:
visit signin_path
fill_in 'email', with: user.email
fill_in 'password', with: user.password
click_button "Log in"
Just edit it according to your login page structure.
I don't know about minitest, but in rspec I'd create the separate method with this codedef sign_in...end and put it to support\utilities.rb.
Then your code would be looking like that:
describe "UsersAcceptanceTest" do
let(:user) { Factory(:user) }
subject { page }
it "must load and include content" do
sign_in user
visit users_path
it { should have_link("cPanel", href: cpanel_path) }
end
end
As you can see I've edited your code a little bit more.