未定义的方法“ has_selector?”用于rspec装饰器 [英] undefined method 'has_selector?' for rspec decorators
问题描述
我有以下rspec测试:
#spec / decorators / user_decorator_spec.rb
需要File.expand_path 'spec / spec_helper'
描述UserDecorator做
let(:user){UserDecorator.new build(:user,level:build(:level))}
subject {user }
its(:avatar){应该具有_selector'h6'}
结尾
而我得到错误:
失败/错误:its(:avatar){应该具有_selector'h6'}
$ p $中p>
NoMethodError:
未定义的方法 has_selector?,用于#< ActiveSupport :: SafeBuffer:0x007fecbb2de650>
#./spec/decorators/user_decorator_spec.rb:7:in在< top(必填)>'
在{ApplicationController.new之前,我尝试了以下流行建议:
.set_current_view_context}
但随后会显示
未定义的方法set_current_view_context
。我正在使用rspec 2.14.1
和水豚2.0.1
。另外,最奇怪的是-当我在某个帮助程序规范中编写此测试时,它通过了,没有问题...
Help ...
解决方案对于使用此线程中提出的
type :: view
解决方案,我感到不满意,尽管这是非常有效的问题是,我在lib /
目录中的文件的规范文件实际上不应被视为视图规范。
所以我在互联网上做了一些进一步的研究,发现对我来说似乎是一种更好的方法。
因此,请考虑该线程中的原始示例。您只需在describe块内添加以下行:
include Webrat :: Matchers
所以最后一个例子如下:
#spec / decorators / user_decorator_spec.rb
require File.expand_path'spec / spec_helper'
描述UserDecorator do
include Webrat :: Matchers
let(:user){UserDecorator.new build(:user,level:build(:level))}
主题{user}
its(:avatar){应该具有h_selector'h6 '}
结束
I have the following rspec test:
# spec/decorators/user_decorator_spec.rb require File.expand_path 'spec/spec_helper' describe UserDecorator do let(:user) { UserDecorator.new build(:user, level: build(:level)) } subject { user } its(:avatar) { should have_selector 'h6' } end
and I get error:
Failure/Error: its(:avatar) { should have_selector 'h6' } NoMethodError: undefined method `has_selector?' for #<ActiveSupport::SafeBuffer:0x007fecbb2de650> # ./spec/decorators/user_decorator_spec.rb:7:in `block (2 levels) in <top (required)>'
I have tried the popular suggestion with:
before { ApplicationController.new.set_current_view_context }
but then it says
undefined method set_current_view_context
. I'm usingrspec 2.14.1
andcapybara 2.0.1
. Also, the strangest thing - when I wrote this test in some helper spec it passed, no problems...Help...
解决方案I didn't feel very satisfied about using the
type: :view
solution proposed in this thread, despite it's a very valid one. The thing is that my spec file for a file insidelib/
directory shouldn't be actually considered as a view spec.So I did some further research over the internet and found out one that seems a better approach to me.
So considering the original example in this thread. You should just add the following line inside your describe block:
include Webrat::Matchers
So the final example would look like:
# spec/decorators/user_decorator_spec.rb require File.expand_path 'spec/spec_helper' describe UserDecorator do include Webrat::Matchers let(:user) { UserDecorator.new build(:user, level: build(:level)) } subject { user } its(:avatar) { should have_selector 'h6' } end
这篇关于未定义的方法“ has_selector?”用于rspec装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!