测试 描述一段测试代码

runup · April 29, 2014 · Last by debugger replied at April 29, 2014 · 2433 hits

下面一段测试代码看不太懂,看的较为透彻的能否帮忙解释下: 背景介绍: 代码来自于《every rails testing with rspec》中的第四章节最后一部分,其中主要是对模型 contact 和模型 phone 两个进行测试,模型之间的关系是 contact has_many phones ,phone 分为三种类型,分别是 home_phone,work_phone,mobile_phone 三种类型。

测试代码如下:

FactoryGirl.define do
    factory :contact do
        firstname { Faker::Name.first_name }
        lastname { Faker::Name.last_name }
        email { Faker::Internet.email }
        after(:build) do |contact|
            [:home_phone, :work_phone, :mobile_phone].each do |phone|
                contact.phones << FactoryGirl.build(:phone,
                    phone_type: phone, contact: contact)
            end
        end
    end
end

肿么了?你需要看下 factory_girl 和 faker 的文档。

#1 楼 @etnl #2 楼 @debugger 哈哈,原来如此,需要借助专门的文档帮助才能看懂~

#2 楼 @debugger #1 楼 @etnl

# Define a factory that calls the generate_hashed_password method after it is built
factory :user do
  after(:build) { |user| generate_hashed_password(user) }
end

能否帮我解释下 after(:build) 在这里的作用?感谢前辈

#4 楼 @runup 字面上就可以理解吧,build 之后,产生 user 的密码。

介个,准确的说应该叫测试前的数据准备工作,还没到测试的正题上

着没到测试了,只是在给数据打桩 (stub),核心的测试代码一行没有啊。 测试注重的你的思路,有思路了再想着如何去写测试。

You need to Sign in before reply, if you don't have an account, please Sign up first.