1

尝试测试用户登录(管理员)然后创建更多用户的场景。

在日志中,我可以看到控制进入登录页面,然后管理员用户登录,当控制重定向到进一步的用户创建页面时,登录过滤器停止并将控制重定向回登录页面。

黄瓜新手,所以代码质量不好,所以任何测试登录用户服务的指南都会有所帮助

这是我的场景

Feature: Create user from LMS
  In order to create lms user with multiple groups
  As a author
  I want to create lms user with multipl groups

  Scenario: Add  new user with multiple groups
      Given the following user information
      And I am logged in as author "gulled" with password "thebest"
      When I request for new lms user creation
      Then the new user "user1" should be created

这是定义

Given /^the following user information$/ do 
  # Factory(:login)
  # Factory(:author)
end

Given /^I am logged in as author "([^"]*)" with password "([^"]*)"$/ do |username, password|
  visit "account/login"
  fill_in "loginfield", :with => username   
  fill_in "password", :with => password
  click_button "submit_button"  
end

When /^I request for new lms user creation$/ do
  visit "/author_backend_lms/new_user"  
  fill_in "login_first_name", :with => ""
  fill_in "login_last_name", :with => ""
  fill_in "login_login", :with => ""
  fill_in "login_email", :with => ""
  fill_in "login_password_confirmation", :with => ""
  click_button "create_user_form_submit_button"
end

Then /^the new user "([^"]*)" should be created$/ do |user_login|
  login = Login.find_by_login user
  assert_no_nil login, "Record creation failed" 
end

在“请求创建新的 lms 用户”中,当尝试访问 lms 用户创建页面时,控件会重定向回登录页面。

这是我的宝石测试清单

gem "capybara", "1.1.1"
gem "cucumber", "1.1.0"
gem "cucumber-rails", "0.3.2"   
4

2 回答 2

0

我有一个示例项目,它说明了使用 Cucumber 做这类事情的更好(IMO)方法。我认为它可能会提供您要求的一些指导

看这里

希望它有用

于 2013-01-21T20:40:27.520 回答
0

看起来您没有在该Given the following user information步骤中事先创建管理员用户,这导致该And I am logged in as author "gulled" with password "thebest"步骤失败。

尝试使用save_and_open_page方法来调试每个步骤之后发生的事情。

我将重写场景如下(没有太多不需要的细节):

Scenario: Add  new user with multiple groups
  Given I am logged in as an admin user
  When I request for new lms user creation
  Then a new user should be created 

请查看http://aslakhellesoy.com/post/11055981222/the-training-wheels-came-off以获得一些关于如何编写更好场景的好建议。

编辑

这是我的一个项目中的 step_definitions 示例,用于预先创建用户并登录:

Given /^the user has an account$/ do
  @user = FactoryGirl.create( :user )
end

When /^the user submits valid signin information$/ do
  fill_in "user_email",    with: @user.email
  fill_in "user_password", with: @user.password 
  click_button "Sign in"
  page.should have_link('Logout', href: destroy_user_session_path)
end

使用实例变量使用户工厂对象跨步骤持续存在。logout并检查步骤末尾是否有链接可确保登录确实成功。希望这有助于微调您的步骤定义。

于 2013-01-13T14:01:00.463 回答