测试设计用黄瓜登录
问题描述:
我知道理想的做法是填写登录表单并遵循该流程。问题是我没有使用设计进行登录。我使用Facebook和fb_graph gem进行身份验证后,在我的应用程序中登录了用户。测试设计用黄瓜登录
因此,设计的sign_in视图只有链接“连接Facebook”,但我能够看到该路线,并且我假设在用户登录该网址时会尝试登录该用户。
我试图做一个邮寄到sign_in(这观点是空的)直接用黄瓜,和即使响应是确定的,用户没有登录。
Given /^I am a logged in user$/ do
@user = Factory(:user)
res = post("https://*.com/users/sign_in", :email => @user.email, :password => "password")
p res
end
如何测试呢?
感谢,
UPDATE:
的情况是这样的:
Scenario: Going to the index page
Given I am a logged in user
And there is a subject created
And there is 1 person for that subject
When I go to that subject persons index page
And show me the page
Then I should see "Back to Subjects list"
答
而是这样做的,这我不是我骄傲落得这样做如下:
应用控制器
before_filter :authenticate_user!, :except => [:login]
# This action is supposed to only be accessed in the test environment.
# This is for being able of running the cucumber tests.
def login
@user = User.find(params[:id])
sign_in(@user)
current_user = @user
render :text => "user logged in"
end
路线
# This is for being able of testing the application with cucumber. Since we are not using devise defaults login
match 'login/:id' => 'application#login', :as => 'login', :via => [:get] if Rails.env.test?
使用者步骤
Given /^I am a logged in (student|employee)+ user$/ do |role|
@user = @that = Factory(:user, :role => role, :name => "#{role} User Name")
Given("that user is logged in")
end
Given /^that user is logged in$/ do
Given("I go to that users login page")
end
路径
when /that users login page/
login_path(@that || @user)
这样,在我的情况下,我只需要键入:
Given I am a logged in student user
,其余的只是正常的黄瓜......
答
我不得不说,这是我想出了一些讨厌的猴子补丁。
添加到我的application_controller。
if Rails.env.test?
prepend_before_filter :stub_current_user
# UGLY MONKEY PATCH. we need a current user here.
def stub_current_user
unless user_signed_in?
@user = Factory(:user)
sign_in(@user)
current_user = @user
end
end
end
记住我的应用程序中没有sign_in表单,而我正在使用devise。也许以后,我会尝试寻找更好的方法,但现在,这让我完成了一些事情。