我正在看这个宝石: https ://github.com/stympy/faker
我用的挺多的,很好用,但是不明白为什么它的所有方法都组织成模块:
Faker::Code.isbn #=> "759021701-8"
Faker::Address.longitude #=> "-156.65548382095133"
Faker::Bitcoin.address #=> "1HUoGjmgChmnxxYhz87YytV4gVjfPaExmh"
我必须在我的工厂这样做:
factory :person, class: Person do
name { Faker::Lorem.word }
account { Faker::Bitcoin.address }
address { Faker::Address.longitude }
favourite_book { Faker::Code.isbn }
end
我可以将所有模块包含到我的测试套件中,所以我只需要这样做:
factory :person, class: Person do
name { word }
account { address }
address { longitude }
favourite_book { isbn }
end
我正在使用 rspec:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
# doesn't work: config.include Faker
end
为什么他们费心将他们的方法分成不同的方法?让它更快?除非你有一个非常复杂的项目,否则这肯定是一个微不足道的数额?难道不是更好的宝石让你做BetterFaker.word
,BetterFaker.address
等等?更容易记住:)