[url=http://www.pmamediagroup.com/2009/04/tutorial-install-rspec-rails-factory-girl/]另外一个Rspec和Factory Girl的用法示例[/url]
I think you all know that, when using fixtures in your test, you keep switching between files to see what fixtures there are, always struggling with dependencies and conflicts.
There are a couple of helpers that solve this, but this is the best one I came across until know: Factory Girl
As the names says, it provides some kind of factory for your objects and instances.
The following snippet illustrates the definition of such a factory:
Now, this factory can be used in your tests:
I think you get the basic idea behind it, it’s actually pretty clean and makes tests a lot more readable without looking at fixtures.
Note: At the time of this writing, the attribute name does not work since there is a conflict (that will be resolved soon). Instead you have to use:
I think you all know that, when using fixtures in your test, you keep switching between files to see what fixtures there are, always struggling with dependencies and conflicts.
There are a couple of helpers that solve this, but this is the best one I came across until know: Factory Girl
As the names says, it provides some kind of factory for your objects and instances.
The following snippet illustrates the definition of such a factory:
# define an incremental username
Factory.sequence :user do |n|
"user#{n}"
end
# define a user factory
Factory.define :user do |u|
u.admin false
u.username { Factory.next(:user) } # lazy loaded
end
# define a project factory with associated user
Factory.define :project do |p|
p.title 'myproject'
p.creator {|a| a.association(:user) } # again lazy loaded
end
Now, this factory can be used in your tests:
it "should do something" do
Factory.create(:user) # creates a user
Factory(:user) # creates another user (note the shortcut)
@u = Factory.build(:user, :username => 'customuser') # only build, no save
Factory.create (:project, :creator => @u)
end
I think you get the basic idea behind it, it’s actually pretty clean and makes tests a lot more readable without looking at fixtures.
Note: At the time of this writing, the attribute name does not work since there is a conflict (that will be resolved soon). Instead you have to use:
Factory.define :project do |p|
p.add_attribute :name, "myname"
end