如何使用具有持久角色的 Cucumber/Factory Girl

发布于 2024-11-27 12:12:27 字数 2389 浏览 0 评论 0原文

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

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

使用默认配置文件...功能:登录才能访问 到网站的受保护部分 有效的用户应该能够 登录

<块引用>

场景:用户未注册# features/users/sign_in.feature:6 未注册:角色 (参数错误)
/Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory_girl-2.0.0.rc4/lib/factory_girl/registry.rb:15:in 查找'
/Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory_girl-2.0.0.rc4/lib/factory_girl.rb:39:in
工厂名称'
/Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory_girl-2.0.0.rc4/lib/factory_girl/syntax/vintage.rb:53:in default_strategy'
/Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory_girl-2.0.0.rc4/lib/factory_girl/syntax/vintage.rb:146:in
工厂'
/Users/x/rails/ply_rails/features/support/db_setup.rb:6:in ‘之前’ 鉴于我还没有登录# 功能/step_definitions/user_steps.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 "[email protected]"
      When I go to the sign in page
      And I sign in as "[email protected]/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

不知道从哪里开始让这个工作,感谢您的帮助/时间!

I use db/seeds.rb to populate my database with 2 user roles ("Admin", "User") that will never change. When i run tests though, the seed data does not get carried over and the results are error tests.

When i try to run cucumber i get the following error:

Using the default profile... 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 #
features/users/sign_in.feature:6 Not registered: role
(ArgumentError)
/Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory_girl-2.0.0.rc4/lib/factory_girl/registry.rb:15:in
find'
/Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory_girl-2.0.0.rc4/lib/factory_girl.rb:39:in
factory_by_name'
/Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory_girl-2.0.0.rc4/lib/factory_girl/syntax/vintage.rb:53:in
default_strategy'
/Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory_girl-2.0.0.rc4/lib/factory_girl/syntax/vintage.rb:146:in
Factory'
/Users/x/rails/ply_rails/features/support/db_setup.rb:6:in
`Before'
Given I am not logged in #
features/step_definitions/user_steps.rb:36

Here is what my setup looks like:

# 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 "[email protected]"
      When I go to the sign in page
      And I sign in as "[email protected]/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

Not sure where to start on getting this working, thank you for your help/time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2024-12-04 12:12:27

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

其次,就黄瓜而言,您应该定义一个背景块来进行设置。这将针对每个场景运行,并且具有每个操作都明确已知的好处。如果您使用黄瓜场景纯文本向客户端显示,这尤其有用。因此,您应该执行以下操作:

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

Scenario:
    etc

并为角色 [blank] 存在制定自定义步骤,以便为您创建角色。

Well the point of tests is to start with a clean database, i.e a consistent state, so it's kind of good that everything gets wiped.

Secondly, in terms of cucumber, you should be defining a Background block to do the set up. This will run for each scenario, and has the benefit of every action being explicitly known. This is especially useful if you use the cucumber scenario plain text to show to clients. So you should do something like:

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

Scenario:
    etc

And make custom steps for the that the role [blank] exists that will create the role for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文