在使用rspec测试另一个帮助程序时,我遇到使用字体awesome fa_icon
帮助程序的问题。在应用程序中,帮助程序中的帮助程序按预期工作;但是,当我为使用fa_icon
的帮助程序运行我们的rspec测试时,它会失败。
这是助手所做的简化版本:
def helper
if condition
fa_icon "some-icon"
else
fa_icon "some-other-icon"
end
end
这是一个失败,指出fa_icon
是一个未定义的方法。
Failures:
1) ApplicationHelper#risk_flag risk level is below 3
Failure/Error: subject { risk_flag(risk_level) }
NoMethodError:
undefined method `fa_icon' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4::Nested_1:0x000000081ed900>
# ./app/helpers/application_helper.rb:84:in `risk_flag'
# ./spec/helpers/application_helper_spec.rb:79:in `block (3 levels) in <top (required)>'
# ./spec/helpers/application_helper_spec.rb:83:in `block (4 levels) in <top (required)>'
我是否需要以某种特殊方式在规范支持文件中包含font-awesome-rails?我已经将它包含在所有环境中的Gemfile中。
目前使用ruby 2.1.1p176,这里是bundle list | grep '\(rails\)\|\(awesome\)'
Gems included by the bundle:
* actionmailer (4.0.4)
* actionpack (4.0.4)
* activemodel (4.0.4)
* activerecord (4.0.4)
* activerecord-deprecated_finders (1.0.3)
* activesupport (4.0.4)
* acts-as-taggable-on (3.1.1)
* addressable (2.3.6)
* arel (4.0.2)
* bcrypt (3.1.7)
* bourbon (3.2.0)
* builder (3.1.4)
* bullet (4.8.0)
* bundler (1.6.1)
* cancan (1.6.10)
* capybara (2.2.1)
* capybara-webkit (1.1.0)
* chosen-rails (1.1.0)
* chunky_png (1.3.0)
* coderay (1.1.0)
* coffee-rails (4.0.1)
* coffee-script (2.2.0)
* coffee-script-source (1.7.0)
* compass (0.12.6)
* compass-rails (1.1.7)
* countries (0.9.3)
* css_parser (1.3.5)
* cucumber (1.3.14)
* cucumber-rails (1.4.0)
* currencies (0.4.2)
* daemons (1.1.9)
* database_cleaner (1.2.0)
* devise (3.2.4)
* diff-lcs (1.2.5)
* docile (1.1.3)
* dotenv (0.11.0)
* dotenv-deployment (0.0.1)
* easy_translate (0.5.0)
* erubis (2.7.0)
* eventmachine (1.0.3)
* execjs (2.0.2)
* factory_girl (4.4.0)
* factory_girl_rails (4.4.1)
* faker (1.3.0)
* fakeweb (1.3.0)
* font-awesome-rails (4.0.3.1)
* foreman (0.63.0)
* fssm (0.2.10)
* geocoder (1.2.0)
* gherkin (2.12.2)
* globalize (4.0.1)
* headless (1.0.1)
* highline (1.6.21)
* hike (1.2.3)
* i18n (0.6.9)
* i18n-tasks (0.3.11)
* jquery-placeholder-rails (2.0.7)
* jquery-rails (3.1.0)
* jquery-ui-rails (4.2.1)
* json (1.8.1)
* jwt (0.1.11)
* kaminari (0.15.1)
* kgio (2.9.2)
* launchy (2.4.2)
* libv8 (3.16.14.3)
* mail (2.5.4)
* memcachier (0.0.2)
* method_source (0.8.2)
* mime-types (1.25.1)
* mini_portile (0.5.3)
* minitest (4.7.5)
* multi_json (1.9.2)
* multi_test (0.1.1)
* neat (1.5.1)
* nokogiri (1.6.1)
* normalize-rails (3.0.1)
* orm_adapter (0.5.0)
* pg (0.17.1)
* phony (2.2.5)
* phony_rails (0.6.1)
* polyglot (0.3.4)
* pry (0.9.12.6)
* pry-rails (0.3.2)
* quiet_assets (1.0.2)
* rack (1.5.2)
* rack-test (0.6.2)
* railroady (1.1.1)
* rails (4.0.4)
* rails_12factor (0.0.2)
* rails_serve_static_assets (0.0.2)
* rails_stdout_logging (0.0.3)
* railties (4.0.4)
* raindrops (0.13.0)
* rake (10.3.1)
* ref (1.0.5)
* roadie (2.4.3)
* rspec-core (2.14.8)
* rspec-expectations (2.14.5)
* rspec-mocks (2.14.6)
* rspec-rails (2.14.2)
* sass (3.2.19)
* sass-rails (4.0.3)
* selectivizr-rails (1.1.2)
* shoulda-matchers (2.6.0)
* simple_form (3.0.2)
* simplecov (0.8.2)
* simplecov-html (0.8.0)
* simplecov-rcov (0.2.3)
* slop (3.5.0)
* sprockets (2.10.1)
* sprockets-rails (2.0.1)
* term-ansicolor (1.3.0)
* terminal-table (1.4.5)
* therubyracer (0.12.1)
* thin (1.6.2)
* thor (0.19.1)
* thread (0.1.4)
* thread_safe (0.3.3)
* tilt (1.4.1)
* timecop (0.7.1)
* tins (1.1.0)
* treetop (1.4.15)
* twilio-ruby (3.11.5)
* tzinfo (0.3.39)
* uglifier (2.5.0)
* unicorn (4.8.2)
* uniform_notifier (1.4.0)
* warden (1.2.3)
* xpath (2.0.0)
任何帮助将不胜感激。谢谢!
答案 0 :(得分:3)
rspec人能够解决我的问题:
https://github.com/rspec/rspec-rails/issues/1007#issuecomment-41510827
require 'spec_helper'
describe ApplicationHelper do
describe "#risk_flag" do
context "with risk level below 3" do
it "generates a smile icon" do
expect(helper.risk_flag(2)).to eq("<i class=\"fa fa-smile-o\"></i>")
end
end
end
end
主要的变化是我需要从helper