2016-10-11 2 views
0

私はRailsを使用しないActiveRecord Rubyアプリケーションを書いています。 FactoryGirlでモデルをテストしたいのですが、私は設定に苦労しています。私はそれがlib/modelsフォルダ内のモデルを見つけることができないと推測して、初期化されていない定数エラーが与えられた。RailsのないFactoryGirlでRSpec - モデルを探すのはどこですか?

rspec/FactoryGirlのどのフォルダ構造が予想されるのか、モデルの場所についてどのように伝えたいのですか。

RSpecの-fd仕様/モデル/ user_spec.rb
…/spec/models/user_spec.rb:3:in `<top (required)>': uninitialized constant User (NameError) 
    from /usr/local/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `load' 
    from /usr/local/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files' 
    from /usr/local/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `each' 
    from /usr/local/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `load_spec_files' 
    from /usr/local/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:100:in `setup' 
    from /usr/local/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:86:in `run' 
    from /usr/local/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:71:in `run' 
    from /usr/local/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:45:in `invoke' 
    from /usr/local/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/exe/rspec:4:in `<top (required)>' 
    from ./bin/rspec:17:in `load' 
    from ./bin/rspec:17:in `<main>' 

仕様/モデル/ user.rb
describe User, type: :model do 
    it 'can be instantiated' do 
    user = User.new 
    expect(user).to be_a User 
    end 
end 
のlib /モデル/ user.rb
class User < ActiveRecord::Base 
end 

私はRSpecのを作成しましたコンフィグレーション:rspec --init

.rspec
--color 
--require spec_helper 
スペック/ spec_helper.rb
require 'factory_girl' 
require_relative 'support/factory_girl' 

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 

end 
仕様/サポート/ factory_girl.rb
RSpec.configure do |config| 
    config.include FactoryGirl::Syntax::Methods 

    config.before(:suite) do 
    FactoryGirl.find_definitions 
    end 
end 
スペック/ factories.rb
FactoryGirl.define do 
    factory :user do 
    username "johnny" 
    end 
end 

答えて

0

あなたが不足している部分があるというRailsの負荷モデルのすべてRSpecテストスイートを実行しているときはFactoryGirlよりspec_helper.rbです。

があなたのspec_helper.rb

root = File.expand_path('../', File.dirname(__FILE__)) 
Dir[File.join(root,'lib/**/*.rb')].each { |f| require f } 

require 'factory_girl' 
require_relative 'support/factory_girl' 

... 
+0

感謝の上部にあなたのすべてのモデルをロードしてください、これは私を助けて。 'Dir [File.join(root、 'lib/**/*。rb')]。each {| f | | | | | | | |}必要なf} 'モデルの1つに' ActiveRecord :: ConnectionNotEstablished:IDがプライマリである接続プールが見つかりませんでした 'というclosure_treeが含まれているため、別の問題が発生しています。 –

+0

結合メソッドのエラーを修正するコードを更新しました。もう1つのエラーは別の問題です。それを推測するのは難しいですが、あなたの 'users'テーブルに主キー' id'が含まれていますか? – Midwire

関連する問題