Created
January 31, 2014 00:47
-
-
Save Jamedjo/8723322 to your computer and use it in GitHub Desktop.
Capybara sign in/up helpers for use with Cucumber. Uses FactoryGirl to get sign up attributes, and to create a user for signing in.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SignInHelpers | |
def sign_up_as(user_type, form_params={}) | |
attributes = FactoryGirl.attributes_for(user_type).merge(form_params) | |
destory_user(attributes[:email]) | |
visit '/users/sign_up' | |
within("#new_user") do | |
fill_in "Name", :with => attributes[:name] | |
fill_in "Email", :with => attributes[:email] | |
fill_in "Password", :with => attributes[:password] | |
fill_in "Password confirmation", :with => attributes[:password_confirmation] | |
end | |
click_button "Sign up" | |
# return attributes[:email] | |
end | |
def sign_in_as(user_type, form_params={}, create_params={}) | |
user = FactoryGirl.create(user_type, create_params) | |
visit '/' | |
fill_in "Username or Email", :with => form_params[:email] || user.email | |
fill_in "Password", :with => form_params[:password] || user.password | |
click_button "Sign in" | |
end | |
def sign_up(params={}) | |
sign_up_as(:user, {email: "[email protected]"}.merge(params)) | |
end | |
def sign_in(params={}) | |
sign_in_as(:user, params, email: "[email protected]") | |
end | |
private | |
def destory_user(email) | |
user = User.where(email: email).first | |
user.destroy unless user.nil? | |
end | |
end | |
World(SignInHelpers) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given(/^I am logged in$/) do | |
sign_in | |
end | |
When(/^I sign up without a password confirmation$/) do | |
sign_up(password_confirmation: "") | |
end | |
When(/^I try to sign in as a user who doesn't exist$/) do | |
sign_in(email: "[email protected]") | |
end | |
When(/^I sign out$/) do | |
visit '/users/sign_out' | |
end | |
Given(/^I am logged in as an administrator$/) do | |
sign_in_as(:admin) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment