我正确使用工厂了吗?
这是我当前的测试设置:
# spec/factories.rb
require 'factory_girl'
FactoryGirl.define do
# Roles
factory :user_role, :class => Role do
name 'User'
end
# Users
factory :user, :class => User do
sequence(:email) {|n| "email#{n}@example.com" }
password 'password'
password_confirmation 'password'
name 'Yuri Userington'
roles { |a| [a.association(:user_role)] }
end
# Instruments
factory :instrument, :class => Instrument do
title "Doobie Doo Instrument Title"
is_valid true
association :user, :factory => :user
end
# Sequences
sequence :email do
"email#{n}@factory.com"
end
end
# spec/controllers/instruments_controller_spec.rb
require 'spec_helper'
describe InstrumentsController do
before (:each) do
@instrument = FactoryGirl.create(:instrument)
@attr = FactoryGirl.attributes_for(:instrument)
@user = FactoryGirl.create(:user)
end
describe "GET index" do
it "assigns all instruments as @instruments" do
instrument = Instrument.new(@attr)
instrument.user = @user
instrument.save!
get :index
assigns(:instruments).should eq([instrument])
end
end
end
结果是,当我运行测试时,我在输出中收到以下错误:
Failures:
1) InstrumentsController GET index assigns all instruments as @instruments
Failure/Error: @instrument = FactoryGirl.create(:instrument)
ActiveRecord::RecordNotFound:
Couldn't find Role with id=2
# ./app/models/user.rb:21:in `assign_role_after_sign_up'
# ./spec/controllers/instruments_controller_spec.rb:24:in `block (2 levels) in <top (required)>'
基于此,我的 :user 工厂中的角色关联调用似乎没有被调用 - 我是什么这里做错了吗?我是否以完全错误的方式使用它?
谢谢你!!
This is my current testing setup:
# spec/factories.rb
require 'factory_girl'
FactoryGirl.define do
# Roles
factory :user_role, :class => Role do
name 'User'
end
# Users
factory :user, :class => User do
sequence(:email) {|n| "email#{n}@example.com" }
password 'password'
password_confirmation 'password'
name 'Yuri Userington'
roles { |a| [a.association(:user_role)] }
end
# Instruments
factory :instrument, :class => Instrument do
title "Doobie Doo Instrument Title"
is_valid true
association :user, :factory => :user
end
# Sequences
sequence :email do
"email#{n}@factory.com"
end
end
# spec/controllers/instruments_controller_spec.rb
require 'spec_helper'
describe InstrumentsController do
before (:each) do
@instrument = FactoryGirl.create(:instrument)
@attr = FactoryGirl.attributes_for(:instrument)
@user = FactoryGirl.create(:user)
end
describe "GET index" do
it "assigns all instruments as @instruments" do
instrument = Instrument.new(@attr)
instrument.user = @user
instrument.save!
get :index
assigns(:instruments).should eq([instrument])
end
end
end
The result is that when i run my tests, i get the following errors in my output:
Failures:
1) InstrumentsController GET index assigns all instruments as @instruments
Failure/Error: @instrument = FactoryGirl.create(:instrument)
ActiveRecord::RecordNotFound:
Couldn't find Role with id=2
# ./app/models/user.rb:21:in `assign_role_after_sign_up'
# ./spec/controllers/instruments_controller_spec.rb:24:in `block (2 levels) in <top (required)>'
Based on that it seems like the roles association call in my :user factory is NOT being called -- what am i doing wrong here? Am i using this in a completely wrong way?
thank you!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有很多话要说。将您的代码与以下代码进行比较,看看删除了多少行或单词。
在您的测试中:
此外:
我个人更喜欢使用模拟和存根测试控制器
我使用
let
而不是实例变量,before_filter
There is much to say here. Compare your code with the following to see how many lines or words were removed.
And in your tests:
Moreover:
I personally prefer testing controller with mocks and stubs
I use
let
instead of instance variables andbefore_filter
我遇到了类似的问题,我使用回调来分配这样的角色:
所以我认为你应该能够做类似的事情:
这是完全未经测试的,所以你可能需要调整一些事情。
I had a similar issues and I used a callback to assign roles like this:
So I think you should be able to do something akin to that:
That is completely untested, so you may need to tweak things around.