rspec / rspec-rails

RSpec for Rails 6+
https://rspec.info
MIT License
5.18k stars 1.04k forks source link

Font-awesome-rails helper not found within rspec #1007

Closed GolfyMcG closed 10 years ago

GolfyMcG commented 10 years ago

I'm experiencing a problem with using the font awesome fa_icon helper when testing another helper with rspec. Within the app, the helper within the helper, works as intended; however, when I run our rspec tests for the helper that uses fa_icon it fails.

This is a simplified version of what the helper does:

def helper
  if condition
    fa_icon "some-icon"
  else
    fa_icon "some-other-icon"
  end
end

Here is one of the failures stating that fa_icon is an undefined method.

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)>'

Do I need to include font-awesome-rails in the spec support files in some special way? I've included it in the Gemfile in all environments.

Currently using ruby 2.1.1p176 and here is the output from 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)

Any help would be appreciated. Thank you!

cupakromer commented 10 years ago

Can you create a new simple helper with just your sample and a new spec doing a simple test on it?

I tried locally and I have no issues. I locked into Ruby 2.1.1 and your same rspec gem versions, as well as your font-awesome-rails version.

GolfyMcG commented 10 years ago

So, I solved the problem but accidentally deleted one line and closed out my vim without it checked into version control and I can't recall what I had. These are the relevant bits:

app/helpers/application_helper.rb
def risk_flag(risk_level)
  icon_name = if risk_level > 5
                "frown-o"
              elsif risk_level >= 3 && risk_level <= 5
                "meh-o"
              else
                "smile-o"
              end
  fa_icon(icon_name)
end
spec/helpers/application_helper_spec.rb
describe "#risk_flag" do
  subject { risk_flag(risk_level) }

  context "risk level is below 3" do
    let(:risk_level) { 2 }

    it "should call fa_icon smile" do
      should eq("<i class=\"fa fa-smile-o\"></i>")
    end
  end
end
spec/spec_helper
RSpec.configure do |config|
  config.include FontAwesome::Rails::IconHelper
end

This last snippet is the problem. I'm trying to include the fa_icon module, but I can't seem to remember what I had written to include it. The module, if you viewed it in the font-awesome-rails repo would make you think it should be FontAwesome::Rails::IconHelper but it says that's an invalid constant.

cupakromer commented 10 years ago

@GolfyMcG I see what's going on here. You need to execute your test in the context of the helper instance:

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

Check out the Relish docs on helper specs: https://www.relishapp.com/rspec/rspec-rails/v/2-14/docs/helper-specs/helper-spec

cupakromer commented 10 years ago

You won't need config.include FontAwesome::Rails::IconHelper in your spec helper.

GolfyMcG commented 10 years ago

Sorry for not including the entire file but this is within the block you specified....

cupakromer commented 10 years ago

@GolfyMcG Not sure I follow? I included my full application_helper_spec.rb so you could see what I used. The solution is using helper.risk_flag(...) instead of the bare risk_flag(...).

GolfyMcG commented 10 years ago

My apologies! It was the change in syntax of the test that I didn't see:

should eq("<i class=\"fa fa-smile-o\"></i>")

to

expect(helper.risk_flag(2)).to eq("<i class=\"fa fa-smile-o\"></i>")

All green! Thank you! You rock!