【问题标题】:RSpec with FactoryGirl without Rails – where does it to look for models?RSpec with FactoryGirl without Rails – 在哪里寻找模型?
【发布时间】:2017-02-20 03:43:34
【问题描述】:

我正在编写不使用 Rails 的 ActiveRecord Ruby 应用程序。我想用 FactoryGirl 测试模型,但我在配置方面遇到了困难。鉴于未初始化的常量错误,我猜它在lib/models 文件夹中找不到模型。

我想知道 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/models/user.rb
class User < ActiveRecord::Base
end

我已经使用rspec --init 创建了 RSpec 配置:

.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
规格/工厂.rb
FactoryGirl.define do
  factory :user do
    username "johnny"
  end
end

【问题讨论】:

    标签: ruby activerecord rspec


    【解决方案1】:

    您缺少的部分是 Rails 在运行 RSpec 测试套件时从 spec_helper.rb 加载 FactoryGirl 之前的所有模型。

    尝试在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'
    
    ...
    

    【讨论】:

    • 谢谢,这对我有帮助。虽然第二行抱怨缺少 String 的 join 方法,但此修改似乎有效:Dir[File.join(root,'lib/**/*.rb')].each { |f| require f } 现在我遇到了另一个问题,因为其中一个模型包含 closure_tree 导致 ActiveRecord::ConnectionNotEstablished: No connection pool with id primary found.
    • 我更新了代码来修复join方法错误。另一个错误是一个单独的问题。很难猜到,但您的 users 表是否包含主键 id?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 2023-03-25
    • 2019-05-02
    相关资源
    最近更新 更多