2016-11-21 39 views
1

假設我有以下Rails模型,並且顯示的方法已經過測試。現在你能明確地在FactoryGirl中輸入一個方法的返回值嗎?

class Employee < ActiveRecord::Base 
    has_many :jobs 

    def total_annual_income 
    jobs.collect { |j| j.annual_salary}.sum 
    # Or some other AR magic to do it directly in the database; doesn't matter 
    end 
end 

class Job < ActiveRecord::Base 
    # property :annual_salary 

    belongs_to :employee 
end 

,假設我要去別的地方寫一些其他的方法調用Employee#total_annual_income。當我使用FactoryGirl測試此方法時,是否可以直接使用total_annual_income屬性設置我的Employee工廠,而不必製作相應的Job工廠?即,可我只是做

FactoryGirl.define do 
    factory :employee1, class: Employee do 
    id 100 
    total_annual_income 100000.0 
    end 
end 

代替

FactoryGirl.define do 
    factory :employee1, class: Employee do 
    id 100 
    end 
end 

# WANT TO OMIT THIS ENTIRE SET OF FACTORIES # 
FactoryGirl.define do 
    factory :employee1_job1, class: Job do 
    id 100 
    employee_id 100 
    annual_salary 60000.0 
    end 
    factory :employee1_job2, class: Job do 
    id 101 
    employee_id 100 
    annual_salary 40000.0 
    end 
end 
# WANT TO OMIT THIS ENTIRE SET OF FACTORIES # 

我有點新FactoryGirl還在,所以如果道歉,我忽略了一些基本的東西。

回答

0

看一看工廠女孩文件下的關聯信息:

https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations

這有一個使用#create_list產生的職位列表爲用戶的:user_with_posts一個例子 - 有點像你的列表的工作。由於在計算器上,習慣上包括完整的答案的情況下,外部鏈接應該成爲壞了,這裏的copypasta其意見的例子:對的has_many關係

生成的數據是有點更復雜,這取決於量所需的靈活性,但這裏有一個生成相關數據的絕對例子。

FactoryGirl.define do 

    # post factory with a `belongs_to` association for the user 
    factory :post do 
    title "Through the Looking Glass" 
    user 
    end 

    # user factory without associated posts 
    factory :user do 
    name "John Doe" 

    # user_with_posts will create post data after the user has been created 
    factory :user_with_posts do 
     # posts_count is declared as a transient attribute and available in 
     # attributes on the factory, as well as the callback via the evaluator 
     transient do 
     posts_count 5 
     end 

     # the after(:create) yields two values; the user instance itself and the 
     # evaluator, which stores all values from the factory, including transient 
     # attributes; `create_list`'s second argument is the number of records 
     # to create and we make sure the user is associated properly to the post 
     after(:create) do |user, evaluator| 
     create_list(:post, evaluator.posts_count, user: user) 
     end 
    end 
    end 
end 

這讓我們這樣做:

create(:user).posts.length # 0 
create(:user_with_posts).posts.length # 5 
create(:user_with_posts, posts_count: 15).posts.length # 15 

這樣做的核心是真的以上顯示,#create_list方法。

[編輯] 完全未經檢驗的,我想你的榜樣變成類似:

FactoryGirl.define do 
    factory :employee_with_jobs, class: Employee do 
    id 100 

    transient do 
     jobs_count 2 
    end 

    after(:create) do |employee, evaluator| 
     create_list(:job, evaluator.jobs_count, 
        employee: employee, 
        annual_salary: 40000.0) 
    end 
    end 
end 

create(:employee_with_jobs, jobs_count: 5) # Expecting total salary 200000.0. 

...更多或更少。

相關問題