已解决:原作者曾用 af@#$ing 类变量记忆。
@@roles ||= Role.all
父母们,和你的孩子谈谈 ruby 中的类变量。在别人做之前。
编辑:为用户创建步骤添加了场景和代码。
我正在努力测试一个旧的遗留代码库,在使用 cucumber 时遇到问题。
系统中有“患者”、“站点”和“管理员”用户。有些测试要求您使用患者登录,其他测试要求您使用站点、管理员等登录。当我仅使用已登录的患者运行功能时,它们都可以工作。当我只使用登录的站点用户运行这些功能时,它们都可以工作。当我只使用登录管理员运行功能时,它们都可以工作。当我尝试对多种类型的用户运行混合功能时,第二种和第三种用户的功能不起作用。
我发现的一个线索是,它似乎将所有用户都视为在测试运行中首先登录的用户类型。
这是站点用户的示例测试
Background:
Given that I am an "site" user
Scenario: Patients compliance report page
When I have patient "Joel" with "4" missed days in diary
And I go to compliance report page
Then I should see "Patient Compliance Report"
And I should see columns
| Site #/Investigator |
| Patient |
| Registration Date |
| Compliance % |
后台步骤调用以下方法:
def create_and_login_as_role(role)
role_sym = :"#{role}_user"
diary = FactoryGirl.create :diary
study = FactoryGirl.create :study
site = FactoryGirl.create :site, study: study
user = FactoryGirl.create role_sym, diary: diary, organization: site
login user.username, DEFAULT_PASSWORD
end
def login(username, password)
visit destroy_user_session_path # kills any session, then defaults to login
fill_in 'user[username]', with: username
fill_in 'user[password]', with: password
click_on "Sign in"
end
这是第二种情况
Background:
Given there are two countries
And there is a region
Given that I am an "admin" user
#I have to create 'ted' second because of the stupid hold-on-to-first-user-type bug
And there is a site user "ted"
Scenario: Creating a site user
And I visit the "sites" page
And I click "New Site"
And I select the first study from the list
And I select "USA" from "Country"
And I select "North America" from "Region"
And I fill in "Site Number" with "234"
And I fill in "Site Name" with "SiteySiteful"
And I select "Active" from "Status"
And I select "ted" from "Investigator"
And I select "ted" from "Primary Contact"
And I fill in "password" with "password"
And I click "Create Site"
Then I should see "Site was successfully created"
在这种情况下,该错误还有第二种表现形式。如果我首先创建了站点用户(这只是对工厂的调用),它会以站点用户身份登录管理员用户。
这是用户的工厂:
FactoryGirl.define do
factory :user do |u|
sequence(:email) {|n| "example#{n}@verdacom.com" }
sequence(:username) {|n| "user#{n}"}
password "password"
tmp_password "password"
country_id 1
user_state { FactoryGirl.create(:user_state) }
password_confirmation {|u| u.password}
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
work_phone "0123456789"
mobile_phone "0123456789"
timezone "Etc/UTC"
diary_creation true
diary { FactoryGirl.create(:diary) }
end
factory :admin_user, :parent => :user, :class => User do |s|
sequence(:email) {|n| "admin#{n}@verdacom.com" }
sequence(:username) {|n| "admin#{n}"}
force_update_password false
roles { [FactoryGirl.create(:admin_role)] }
end
factory :diary_author, :parent => :user, :class => User do |s|
diary nil
end
factory :subject_user, :parent => :user, :class => User do |s|
roles {[FactoryGirl.create(:subject_role)]}
diary {FactoryGirl.create(:diary_with_registration_entry)}
# s.organization { |user| user.association :site }
end
factory :site_user, :parent => :user, :class => User do
sequence(:email) {|n| "site#{n}@verdacom.com" }
sequence(:username) {|n| "site#{n}"}
force_update_password false
roles { [FactoryGirl.create(:site_role)] }
# organization { |user| user.association :site }
end
end
这是我的gemfile;值得注意的是 rails 3.1.5、devise、mysql2、activeadmin custom fork(不是我的选择)、cancan、guard、cucumber_rails 和 database_cleaner
source 'http://rubygems.org'
gem 'rails', '3.1.5'
gem 'mysql2'
gem 'activeadmin', :path => "vendor/gems/activeadmin-0.3.4", :git => "https://github.com/alexey/active_admin.git", :branch => "0.3.4-stable" #
gem "haml"
gem "rake", ">= 0.9.2"
gem "meta_search", '>= 1.1.0.pre'
gem 'cancan'
gem 'twiliolib'
gem 'paper_trail'
gem 'jquery-rails'
gem 'capistrano'
gem "capistrano-ext"
gem 'seed-fu', '~> 2.1.0'
gem "timezone"
gem "twilio-ruby"
gem "yettings"
gem "zipruby"
gem "whenever", :require => false
gem "newrelic_rpm"
gem "rabl"
gem 'andand'
gem "execjs"
gem "therubyracer"
gem "hoptoad_notifier", '2.3.11'
gem "faker" # need in development and production/staging also
gem 'sass-rails', '~> 3.1.4'
group :assets do
gem 'coffee-rails', '~> 3.1.1'
gem 'uglifier', '>= 1.0.3'
end
group :test do
gem 'turn', '< 0.8.3'
gem 'cucumber-rails', require: false
gem 'factory_girl_rails'
gem 'launchy'
gem "timecop" # for time travelling to test "entries" timeframe
gem "rack-test"
gem "prawn" # for pdf test report generation
gem "webrat" # simulte http interaction with remote service
end
group :test, :development do
gem "rspec-rails", "~> 2.6"
gem "shoulda"
gem "simplecov" # rcov not supported in ruby 1.9
gem 'capybara-webkit' # for headless testing
gem 'database_cleaner'
gem 'debugger'
gem 'rb-fsevent'
gem 'spork', '~> 1.0rc'
gem 'guard'
gem 'guard-spork'
gem 'guard-rspec'
gem 'guard-cucumber'
gem 'single_test'
end
group :development do
gem 'rails-footnotes', '>= 3.7.5.rc4'
gem "rails-erd"
gem 'pry'
gem 'pry-rails'
gem 'redcarpet'
gem 'quiet_assets'
end
platforms :ruby do
gem 'rb-readline'
end
这是我对黄瓜的 database_cleaner 设置,这正是他们的 github 自述文件中的内容:
begin
require 'database_cleaner'
require 'database_cleaner/cucumber'
DatabaseCleaner.strategy = :truncation
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
Before do
DatabaseCleaner.start
end
After do |scenario|
DatabaseCleaner.clean
end
任何帮助,将不胜感激。请询问任何可能相关的文件。