[英]Draper and RSpec and Factory Girl Controller Specs on Rails 4 App
I am using Draper for the first time and it's working great...except for the fact that it broke most of my specs. 我是第一次使用Draper,它的运行效果非常好……除了它违反了我的大多数规格。 An example: 一个例子:
I am setting up an account_settings_controller_spec.rb 我正在设置一个account_settings_controller_spec.rb
describe '#create' do
before do
@user = create :user
end
end
This worked totally fine before (I have config.include FactoryGirl::Syntax::Methods
set to call the factories like this) 之前,这完全正常(我已经将config.include FactoryGirl::Syntax::Methods
设置为像这样调用工厂)
However now when I call create :user
I get undefined method 'full_name' for class 'User'
which is a method I moved from my User model to my UserDecorator. 但是,现在当我调用create :user
我得到undefined method 'full_name' for class 'User'
这是我从User模型移动到UserDecorator的一种方法。
This is strange as the factory, nor the spec, use the full_name
method. 这很奇怪,因为工厂或规格都使用full_name
方法。
I've tried multiple things: 我已经尝试了多种方法:
ApplicationController.new.set_current_view_context
in the before block (which results in an error) 在before块中的ApplicationController.new.set_current_view_context
(这将导致错误) require 'draper/test/rspec_integration'
to my spec_helper.rb (which does nothing) require 'draper/test/rspec_integration'
对我的spec_helper.rb使用require 'draper/test/rspec_integration'
(不执行任何操作) before/after(:create) { |instance| instance.decorate }
before/after(:create) { |instance| instance.decorate }
添加before/after(:create) { |instance| instance.decorate }
before/after(:create) { |instance| instance.decorate }
to my factory (which also does nothing) before/after(:create) { |instance| instance.decorate }
到我的工厂(它也不执行任何操作) I'm not sure what I am missing here, or even why the error is being thrown. 我不确定我在这里缺少什么,甚至不确定为什么引发错误。 Perhaps my decorators aren't being included when RSpec boots up? 也许在RSpec启动时不包括我的装饰器吗?
Ahh ha! 啊哈!
I knew I was being stupid. 我知道我很傻。
It's because I was using friendly_id 这是因为我使用的是friendly_id
class User < ActiveRecord::Base
extend FriendlyId
friendly_id :slug_candidates, use: :slugged
...
def slug_candidates
[:full_name, :id]
end
end
I just moved the full_name
method back to the model and it works. 我只是将full_name
方法移回模型,并且可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.