用黄瓜测试设计登录

时间:2011-06-01 12:52:36

标签: login devise cucumber

我知道理想的做法是填写登录表单并遵循该流程。问题是我没有使用设计登录。我在使用Facebook和fb_graph gem进行身份验证后,在我的应用程序中登录用户。

所以设计sign_in视图只有链接“与Facebook连接”,但我能够看到该路由,并且我认为如果对该网址发布帖子,将尝试签署用户。

我尝试使用黄瓜直接向sign_in(视图为空)发帖,即使响应正常,用户也没有登录。

Given /^I am a logged in user$/ do
  @user = Factory(:user)
  res = post("/users/sign_in", :email => @user.email, :password => "password")
  p res
end

我该如何测试?

谢谢,

更新:

场景如下:

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"

2 个答案:

答案 0 :(得分:2)

而不是这样做,我并不为此感到自豪,我最终做了以下事情:

应用程序控制器

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

其余的只是正常的黄瓜......

答案 1 :(得分:0)

我不得不说这是我提出的一些讨厌的猴子补丁。

将此添加到我的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表单,而且我正在使用设计。我可能会在以后尝试寻找更好的方法,但就目前来说,这让我已经完成了任务。