Ruby on rails 如何使用黄瓜/工厂女孩扮演持久角色

Ruby on rails 如何使用黄瓜/工厂女孩扮演持久角色,ruby-on-rails,ruby,testing,cucumber,factory-bot,Ruby On Rails,Ruby,Testing,Cucumber,Factory Bot,我使用db/seeds.rb用两个永远不会改变的用户角色(“管理员”、“用户”)填充我的数据库。但是,当我运行测试时,种子数据不会继续,结果是错误测试 当我尝试运行cucumber时,出现以下错误: 使用默认配置文件。。。功能:登录以获得访问权限 要访问站点的受保护部分,有效用户应该能够 登录 场景:用户未注册# 功能/用户/登录。功能:6未注册:角色 (参数错误) /Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory\u girl-2.0.0.rc4/

我使用
db/seeds.rb
用两个永远不会改变的用户角色(“管理员”、“用户”)填充我的数据库。但是,当我运行测试时,种子数据不会继续,结果是错误测试

当我尝试运行cucumber时,出现以下错误:

使用默认配置文件。。。功能:登录以获得访问权限 要访问站点的受保护部分,有效用户应该能够 登录

场景:用户未注册# 功能/用户/登录。功能:6未注册:角色 (参数错误)
/Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory\u girl-2.0.0.rc4/lib/factory\u girl/registry.rb:15:in
find'
/Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory\u girl-2.0.0.rc4/lib/factory\u girl.rb:39:in
工厂名称“
/Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory\u girl-2.0.0.rc4/lib/factory\u girl/syntax/vintage.rb:53:in
default\u策略'
/Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory\u girl-2.0.0.rc4/lib/factory\u girl/syntax/vintage.rb:146:in
工厂'
/Users/x/rails/ply_rails/features/support/db_setup.rb:6:in `在 因为我没有登录# 功能/步骤定义/用户步骤。rb:36

以下是我的设置:

# features/support/db_setup.rb
Before do
  # Truncates the DB before each Scenario,
  # make sure you've added database_cleaner to your Gemfile.
  DatabaseCleaner.clean

  Factory(:role, :name => 'Admin')
  Factory(:role, :name => 'User')
end



# features/users/sign_in.feature
Feature: Sign in
  In order to get access to protected sections of the site
  A valid user
  Should be able to sign in

    Scenario: User is not signed up  # THIS IS LINE 6
      Given I am not logged in
      And no user exists with an email of "user@user.com"
      When I go to the sign in page
      And I sign in as "user@user.com/password"
      Then I should see "Invalid email or password."
      And I go to the home page
      And I should be signed out



# features/step_definitions/user_steps.rb
Given /^I am a "([^"]*)" named "([^"]*)" with an email "([^"]*)" and password "([^"]*)"$/ do |role, name, email, password|
  User.new(:name => name,
            :email => email,
            :role => Role.find_by_name(role.to_s.camelize),
            :password => password,
            :password_confirmation => password).save!
end

不确定从何处开始工作,感谢您的帮助/时间

测试的重点是从一个干净的数据库开始,即一个一致的状态,所以所有的东西都被清除是一件好事

其次,对于cumber,您应该定义一个背景块来进行设置。这将针对每个场景运行,并且有明确知道每个操作的好处。如果您使用纯文本向客户机显示,这尤其有用。所以你应该做一些类似的事情:

Background:
    Given that the role "Admin" exists
    And that the role "User" exists

Scenario:
    etc

并为角色[blank]指定的
创建自定义步骤存在将为您创建角色的

我曾想过这样做,但后来认为,由于我的应用程序的大多数活动都是在登录用户的上下文中进行的,因此我最终会在我测试的几乎每个控制器中重复该
Background
子句。现在考虑一下,我认为这实际上是可以的,因为它使测试更具可读性和明显性,但我只是想知道你(或任何人)对此有何想法。谢谢我自己也喜欢枯燥的编程,但当涉及到测试时,我不喜欢冒太多的风险,而且冗长和明确也可以(再说一遍:只是我的意见)。我认为在多个cucumber特性的背景中重复一些步骤没有什么错。如果你也愿意的话,你可以少一点冗长,在参考数据存在的情况下,做一个类似于
的步骤,并将所有数据都放在那里。我喜欢Cumber测试,因为集成/验收测试更复杂。谢谢!我现在就是这样做的,而且效果很好。