transient doFactoryBot工厂的目的是什么?
我见过很多以下面的东西开头的工厂.
factory :car do
owner nil
other_attribute nil
end
...
Run Code Online (Sandbox Code Playgroud)
我在这个博客上找到了一些信息:http://blog.thefrontiergroup.com.au/2014/12/using-factory-easily-create-complex-data-sets-rails/
但我仍然不完全明白如何以及为什么这样做.我对FactoryBot的体验很小.
任何有使用FactoryBot经验的人都可以分享一些见解吗?
我正在努力巩固我对rails和BDD工作流程的理解,所以我想通过创建其中一个迷你博客来开始,但是使用rspec.现在我有一个ArticlesController和Article模型,以及相关的rspec文件.文章非常简单,只有标题:字符串和内容:文本,而ArticlesController是RESTful - 虽然我手写了MCV for Article,但它基本上和我用脚手架创建它一样.
但是,当我在rspec中为PUT更新编写测试时,我真的不知道我在做什么.我正在使用Factory Girl来创建文章对象,到目前为止我的代码看起来像:
#factories.rb
FactoryGirl.define do
factory :article do
title "a title"
content "hello world"
end
#articles_controller_spec.rb
before(:each) do
@article = Factory(:article)
end
describe "PUT 'update/:id'" do
it "allows an article to be updated" do
@attr = { :title => "new title", :content => "new content" }
put :update, :id => @article.id, :article => @attr
response.should be_successful
end
end
Run Code Online (Sandbox Code Playgroud)
但是我一直在:
Failures:
1) ArticlesController PUT 'update/:id' allows an article to be updated
Failure/Error: response.should be_successful
expected successful? …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的代码模型工厂:
Factory.define :code do |f|
f.value "code"
f.association :code_type
f.association(:codeable, :factory => :portfolio)
end
Run Code Online (Sandbox Code Playgroud)
但是,当我用一个简单的test_should_create_code测试我的控制器时,如下所示:
test "should create code" do
assert_difference('Code.count') do
post :create, :code => Factory.attributes_for(:code)
end
assert_redirected_to code_path(assigns(:code))
end
Run Code Online (Sandbox Code Playgroud)
......测试失败了.未创建新记录.
在控制台中,似乎attributes_for不会返回所有必需的属性,如create.
rob@compy:~/dev/my_rails_app$ rails console test
Loading test environment (Rails 3.0.3)
irb(main):001:0> Factory.create(:code)
=> #<Code id: 1, code_type_id: 1, value: "code", codeable_id: 1, codeable_type: "Portfolio", created_at: "2011-02-24 10:42:20", updated_at: "2011-02-24 10:42:20">
irb(main):002:0> Factory.attributes_for(:code)
=> {:value=>"code"}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
谢谢,
我对rspec和整个TDD方法都很陌生.有人可以解释一下mock和stub之间的区别.我们何时使用它们,何时使用Factory Girl在测试用例中创建对象?
我正在使用设计,rolify和cancan.我也在使用rspec和FactoryGirl进行测试.现在我正在进行一些测试,我想为这些测试定义具有不同角色的用户.以下是我目前对如何使用FactoryGirl进行测试的猜测:
FactoryGirl.define do
factory :user do
name 'Test User'
email 'example@example.com'
password 'please'
password_confirmation 'please'
# required if the Devise Confirmable module is used
confirmed_at Time.now
factory :admin do
self.has_role :admin
end
factory :curator do
self.has_role :curator
end
factory :super_admin do
self.has_role :super_admin
end
end
end
Run Code Online (Sandbox Code Playgroud)
以下是我的一些测试似乎没有正确编写:require'pec_helper'
describe "Pages" do
subject { page }
before do
@newpage = FactoryGirl.create(:page)
@newpage.save
end
context 'not logged in' do
it_behaves_like 'not admin'
end
context 'logged in' do
context 'as user' do
before do …Run Code Online (Sandbox Code Playgroud) 我在Ruby on Rails 3中使用Rspec(2.11.0)和FactoryGirl(4.0.0)进行TDD/BDD.我有一个类别模型的工厂:
FactoryGirl.define "Category" do
factory :category do
name "Foo"
end
end
Run Code Online (Sandbox Code Playgroud)
如果我删除,创建然后在测试环境中迁移数据库我收到此错误:
rake aborted!
Could not find table 'categories'
Run Code Online (Sandbox Code Playgroud)
出现此问题的原因是FactoryGirl期望表已经存在(出于某些奇怪的原因).如果我从我的rails应用程序中删除spec文件夹并且这样做db:migrate,它可以工作.此外,如果我factory-girl-rails从我的标记,Gemfile因为:require => false它也有效(然后我必须发表评论,以便运行rspec).
我在这里找到了一些关于这个问题的信息:https://github.com/thoughtbot/factory_girl/issues/88
我有什么不对劲吗?如何在db:migration任务中"通过"FactoryGirl阶段?
我有一个类,这是一些专门处理行为的其他类的基础:
class Task < ActiveRecord::Base
attr_accessible :type, :name, :command
validates_presence_of :type, :name, :command
# some methods I would like to test
end
Run Code Online (Sandbox Code Playgroud)
CounterTask类继承自Task
class CounterTask < Task
end
Run Code Online (Sandbox Code Playgroud)
这一切都正常,直到我尝试测试基类,因为它必须有一个类型.
FactoryGirl.define do
factory :task do
sequence(:name) { |n| "name_#{n}" }
sequence(:command) { |n| "command_#{n}" }
end
end
Run Code Online (Sandbox Code Playgroud)
您如何测试超类的基本功能?
我有一个裸导轨3应用程序与一个模型,生成使用rails g model User.
我添加了一个工厂(使用factory_girl_rails):
Factory.define :user do |f|
f.email "test@test.com"
f.password "blah"
f.password_confirmation "blah"
f.display_name "neezer"
end
Run Code Online (Sandbox Code Playgroud)
然后我添加了一个测试:
require 'spec_helper'
describe User do
subject { Factory :user }
it "can be created from a factory" do
subject.should_not be_nil
subject.should be_kind_of User
end
end
Run Code Online (Sandbox Code Playgroud)
然后我使用迁移我的数据库rake db:migrate.
然后我使用运行测试rspec spec,测试失败,并带有以下内容:
Failures:
1) User can be created from a factory
Failure/Error: subject { Factory :user }
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./spec/models/user_spec.rb:5:in …Run Code Online (Sandbox Code Playgroud) 我想将参数传递给工厂,该工厂将用于在关联模型上设置属性.相关模型在工厂内创建.
我有一个事务模型,通过连接表TxLink与自己的多对多链接.我想调用link = FactoryGirl.create(:link_red_to_sub, sub: 10, red: 7)哪个将创建两个Transaction对象和一个链接这两个的TxLink.
由于最后的结果,我在下面的关联行中收到错误units: sub.错误是"未定义特征".我试过units: { sub }(用括号)而不是骰子.
factory :tx_link do
units "9.99"
factory :link_red_to_sub do
ignore do
sub 0
red 0
end
units { red }
association :giver, factory: :transaction, units: sub
association :taker, factory: :redemption, units: red
end
end
Run Code Online (Sandbox Code Playgroud) 我一直在努力与has_many/throughFactory Girl 建立关系.
我有以下型号:
class Job < ActiveRecord::Base
has_many :job_details, :dependent => :destroy
has_many :details, :through => :job_details
end
class Detail < ActiveRecord::Base
has_many :job_details, :dependent => :destroy
has_many :jobs, :through => :job_details
end
class JobDetail < ActiveRecord::Base
attr_accessible :job_id, :detail_id
belongs_to :job
belongs_to :detail
end
Run Code Online (Sandbox Code Playgroud)
我的工厂:
factory :job do
association :tenant
title { Faker::Company.catch_phrase }
company { Faker::Company.name }
company_url { Faker::Internet.domain_name }
purchaser_email { Faker::Internet.email }
description { Faker::Lorem.paragraphs(3) }
how_to_apply { Faker::Lorem.sentence }
location "New …Run Code Online (Sandbox Code Playgroud)