2017-01-31 5 views
0

私はrails api bookに従っていますが、コードをエンジンに組み込んでいます。私はrspec specすなわち、すべてのテストを実行したときに、それを見つけるん、しかし単一のテストがロードされているが、すべてのテストでロードされている工場

uninitialized constant Handicap::FactoryGirl 

:テストでは、私はrspec ./spec/controllers/concerns/handicap/authenticable_spec.rb直接すなわち、テストを実行すると、私はエラーを取得するspec/controllers/concerns/handicap/authenticable_spec.rbであり、この

require 'spec_helper' 
require_relative '../../../../app/controllers/concerns/handicap/authenticable.rb' 
    class Authentication 
    include Handicap::Authenticable 
    end 
module Handicap 
    describe Authenticable, type: :controlller do 
    let(:authentication) { Authentication.new } 
    subject { authentication } 

    describe "#current_user" do 
     before do 
     @user = FactoryGirl.create :handicap_user 
     request.headers["Authorization"] = @user.auth_token 
     authentication.stub(:request).and_return(request) 
     end 
     it "returns the user from the authorization header" do 
     expect(authentication.current_user.auth_token).to eql @user.auth_token 
     end 
    end 
    end 
end 

のように見えますFactoryGirl定数とテストは、このgithubのissueによる

undefined local variable or method `request' for #<RSpec::ExampleGroups::HandicapAuthenticable::CurrentUser:0x007ff276ad5988>. 

で失敗します私は< ActionController::Base

class Authentication < ActionController::Base 

認証クラスすなわちにを追加する必要がありますが、私はこれをで追加した場合、私は

uninitialized constant ActionController 

を取得し、私も< Handicap::ApplicationControllerを追加しようとしているが、

uninitialized constant Handicap::ApplicationController 

があります表示されます私の名前空間に何かが間違っている。 3つの症状があります。私自身でテストを実行するとFactoryGirlが見つかりませんが、すべてのテストが実行されたときに検出されます。 2番目は、すべてのテストを実行してもActionControllerが見つからないということです。 3番目は、次の行を追加する必要があります。

require_relative '../../../../app/controllers/concerns/handicap/authenticable.rb' 

テストするモジュールを見つけるには、次の行を追加する必要があります。

名前空間を修正するにはどうすればよいですか?

rails_helper.rbファイルが

ENV['RAILS_ENV'] ||= 'test' 
require File.expand_path('../dummy/config/environment.rb', __FILE__) 
require 'rspec/rails' 
require 'capybara' 
require 'capybara/rails' 
require 'capybara/rspec' 
require 'capybara-screenshot' 
require 'capybara-screenshot/rspec' 
require 'capybara/poltergeist' 
require 'capybara/email/rspec' 
require 'pp' 
require 'chris_api_helpers' 
# Prevent database truncation if the environment is production 
abort("The Rails environment is running in production mode!") if Rails.env.production? 
require 'spec_helper' 
require 'rspec/rails' 
require 'factory_girl_rails' 

ActiveRecord::Migration.maintain_test_schema! 

Shoulda::Matchers.configure do |config| 
    config.integrate do |with| 
    # Choose a test framework: 
    with.test_framework :rspec 
    with.library :rails 
    end 
end 

RSpec.configure do |config| 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    config.use_transactional_fixtures = true 
    config.infer_spec_type_from_file_location! 
    # Filter lines from Rails gems in backtraces. 
    config.filter_rails_from_backtrace! 
    # force test migrations for db:migrate 
    ActiveRecord::Migration.maintain_test_schema! 
    Capybara::Screenshot.prune_strategy = { keep: 20 } 
    Capybara::Screenshot.append_timestamp = false 
    config.include FactoryGirl::Syntax::Methods 
    FactoryGirl.definition_file_paths << File.join(File.dirname(__FILE__), 'factories') 
    FactoryGirl.find_definitions 
    config.include Devise::Test::ControllerHelpers, type: :controller 
end 

で、あなたはモジュールにスペックを置くべきではありません

require 'simplecov' if ENV["COVERAGE"] 
SimpleCov.start do 
    add_filter '/spec/' 
    add_filter '/config/' 
    add_filter '/lib/' 
    add_filter '/vendor/' 
    add_group 'Controllers', 'app/controllers' 
    add_group 'Models', 'app/models' 
    add_group 'Helpers', 'app/helpers' 
    add_group 'Mailers', 'app/mailers' 
    add_group 'Views', 'app/views' 
end if ENV["COVERAGE"] 

RSpec.configure do |config| 
    config.expect_with :rspec do |expectations| 
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true 
    end 
    config.mock_with :rspec do |mocks| 
    mocks.verify_partial_doubles = true 
    end 
    config.shared_context_metadata_behavior = :apply_to_host_groups 
    #http://stackoverflow.com/questions/30859037/suppress-backtrace-for-rspec-3 
    config.backtrace_exclusion_patterns = [ 
     /\/lib\d*\/ruby\//, 
     /bin\//, 
     /gems/, 
     /spec\/spec_helper\.rb/, 
     /lib\/rspec\/(core|expectations|matchers|mocks)/ 
    ] 
end 
+0

なぜあなたはあなたの仕様を 'module'に入れていますか? –

+0

'spec_helper.rb'の外観はどうですか?あなたはそれに適切な 'rails_helper.rb'を含めていますか? –

+0

dan-classon、私はそれがレールエンジンのため、私の仕様をモジュールに入れています。これは標準です。 – Obromios

答えて

0

、rails_helperはとにかくロードされていました。

恥ずかしいが、このQ & Aは、トラブルの簡単なエラーをスポッティングを持っている他の誰かを助けるかもしれません。

0

spec_helper.rbです。これがここの問題の原因です。名前空間のクラスを参照する必要がある場合は、RSpec.describe Handicap::Authenticatableのように参照してください。

一般に、名前空間内にあり、明示的に 'root'スコープから何かを参照する必要がある場合は、その前にダブルコロンを付けることができます。以下のような:それは私のファイルの先頭に、私は「ないrequire 'spec_helper'``. All my other files hadが必要、require 'rails_helper'を持っていた私は、全体のテストスイートを実行したときにrails_helper'``ている必要があることが判明し

module Handicap 
    class Something 
    def do_stuff 
     ::FactoryGirl.create(:person) 
    end 
    end 
end 
+0

私はこれを試して、それは動作しませんでした。コントローラー用のレールジェネレーターは、エンジンで動かすと、仕様をモジュールに入れてしまうので、それは問題ではないと思います。 '' :: FactoryGirl'''は助けられませんでしたが、私は '' :: '' ''が ''他の場所で使われていて、理解していないのを見ていたので、これに感謝しました。 – Obromios

関連する問題