如何在factory_girl工厂内使用devise current_user

时间:2011-07-24 00:04:32

标签: ruby-on-rails devise cucumber factory-bot

我想在使用factory_girl创建的工厂中使用current_user(设计)。

我的环境包含:Ruby 1.8.7 / Rails 2.3.5 / Devise / Cucumber / Pickle / Factory Girl

我有以下型号

用户:

  has_many :for_me, :class_name => "Task", ....details
  has_many :from_me, :class_name => "Task", ....details

任务:

  belongs_to :assigner, :class_name => "User", ....details
  belongs_to :assignee, :class_name => "User", ....details

我正在测试以下情况:

Given the following users exist
      | email    | password |
      | u1@d.com | 123456   |
      | u1@d.com | 123456   |
And users are confirmed
And I am on the sign in page
And I am logged in as "u1@d.com" with password "123456"
When I fill in "task[description]" with "description 1"
And I press "task_submit"
Then I should see "description 1"

我所拥有的工厂是:

Factory.define :user do |f|
  f.first_name          'user_fn'
  f.last_name           'user_ln'
  f.sequence(:username) {|n| "user_#{n}"}
  f.sequence(:email)    {|n| "user_#{n}@domain.com"}
  f.password            '123456'
  f.mobile_number       '0123456798'
end

Factory.define :task do |f|
  f.association :assignee, xxxxxxx
  f.association :assigner, :factory => :user
end

我的问题是我想使用设备的current_user而不是xxxxxxx

1 个答案:

答案 0 :(得分:5)

我猜你不能直接在工厂内编码。

试试这个:

Factory(:task, :assignee => current_user)