我使用db/seeds.rb
使用永远不会更改的2个用户角色(“Admin”,“User”)填充我的数据库。当我运行测试时,种子数据不会被转移,结果是错误测试。
当我尝试运行黄瓜时,我收到以下错误:
使用默认配置文件...功能:登录以获取访问权限 到站点的受保护部分有效用户应该能够 登录
场景:用户未注册# features / users / sign_in.feature:6未注册:角色 (引发ArgumentError)
/Users/x/.rvm/gems/ruby-1.9.2-p180/gems/factory_girl-2.0.0.rc4/lib/factory_girl/registry.rb:15:infind'
factory_by_name'
/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:indefault_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 "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
不知道从哪里开始工作,谢谢你的帮助/时间!
答案 0 :(得分:0)
测试的重点是从一个干净的数据库开始,即一致的状态,所以一切都被抹去是好的。
其次,就黄瓜而言,您应该定义背景块来进行设置。这将针对每个场景运行,并且具有明确知道的每个操作的好处。如果您使用黄瓜方案纯文本向客户显示,这将特别有用。所以你应该这样做:
Background:
Given that the role "Admin" exists
And that the role "User" exists
Scenario:
etc
为that the role [blank] exists
制作自定义步骤,为您创建角色。