【问题标题】:Why is RSpec unable to location the models I've just created?为什么 RSpec 无法定位我刚刚创建的模型?
【发布时间】:2015-08-10 13:05:35
【问题描述】:

当我运行 rspec spec/models/user_spec.rb 进行测试时,我得到了这个错误:

 /home/alex/dev/greenbull/spec/models/user_spec.rb:3:in `<top (required)>':
 uninitialized constant User (NameError)

这是 /spec 文件夹结构

|-models
| |_
|   |-task.rb
|   |-user.rb
|   |-task_spec.rb
|   |-user_spec.rb
|
|-factories
| |_
|   |-tasks.rb
|   |-users.rb
|
|-spec_helper.rb

spec_helper.rb

require 'rubygems'
require 'factory_girl'

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end

spec/models/user.rb

class User
end

为什么会这样?我哪里做错了?

【问题讨论】:

  • 你的规范文件夹中不应该有模型
  • 那么他们应该在哪里?
  • 它们应该在你的应用中定义
  • 它们在 app/models 文件夹中。
  • 那么您的规范文件夹中的user.rb 是什么?

标签: ruby-on-rails rspec tdd automated-tests factory-bot


【解决方案1】:

spec 文件不应该是这样的,应该是这样的

require 'spec_helper'

describe User do
  describe 'my spec' do
    it 'works' do
      expect(true).to eq true
    end
  end
end

【讨论】:

    【解决方案2】:

    应用根文件夹中有一个名为 .rspec 的隐藏文件,我在这里指定了--require rails_helper

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多