在Spork中运行rspec测试时出现NameError

时间:2013-12-03 02:41:58

标签: ruby-on-rails ruby ruby-on-rails-3 rspec spork

在Spork中运行测试时获取NameError。测试工作发现我是否在Spork中运行它。 (我通过RubyMine 6.0运行Spork)

错误是:

NameError: uninitialized constant FileMakerSync::FMStudent
./app/sync/file_maker_sync.rb:7:in `load_student'
./spec/sync/file_maker_sync_spec.rb:8:in `block (4 levels) in <top (required)>'

我的规格位于:

规格/同步/ file_maker_sync_spec.rb

describe FileMakerSync do
  describe "..." do
    context "..." do
      before(:all) do
        @student = FileMakerSync.load_student('....')

      end
   ...
    end

  end
end

哪个电话

/app/sync/file_maker_sync.rb

class FileMakerSync

  def self.load_student(student_id)
    fm_student = FMStudent.find_single(student_id: student_id)
  end
  ...
end

调用

app/models/filemaker/fm_student.rb

class FMStudent < FMBase
  def as_local_model
  end
  ...
end

我收集的问题是在Spork执行期间未正确加载FMStudent。我不太明白。

我是rails的新手,并且不太需要'filename'和默认情况下可加载的内容。也不是spork设置中可能缺少的东西。

我尝试添加一个require

require "app/models/filemaker/fm_student"
class FileMakerSync

但是我得到了这个错误

Exception encountered: #<LoadError: cannot load such file --     app/models/filemaker/fm_student>

最后我的spec_helper

require 'rubygems'
require 'spork'

Spork.prefork do

  if ENV["RUBYMINE_HOME"]
    $:.unshift(File.expand_path("rb/testing/patch/common", ENV["RUBYMINE_HOME"]))
    $:.unshift(File.expand_path("rb/testing/patch/bdd", ENV["RUBYMINE_HOME"]))
  end

  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}


  RSpec.configure do |config|
    config.use_transactional_fixtures = true
    config.infer_base_class_for_anonymous_controllers = false
    config.order = "random"
    config.include Capybara::DSL


    #Mongoid database cleaner
    config.before(:suite) do
      DatabaseCleaner[:mongoid, {:connection => :unit_test}].strategy = :truncation
      DatabaseCleaner[:mongoid, {:connection => :unit_test}].clean_with(:truncation)
    end

    config.before(:each) do
      DatabaseCleaner[:mongoid, {:connection => :unit_test}].start
    end

    config.after(:each) do
      DatabaseCleaner[:mongoid, {:connection => :unit_test}].clean
    end
  end
end

Spork.each_run do
  # This code will be run each time you run your specs.
  FactoryGirl.reload

end

1 个答案:

答案 0 :(得分:1)

根据模型的路径,您的FMStudent类应定义如下:

# app/models/filemaker/fm_student.rb
class Filemaker::FMStudent < FMBase
  def as_local_model
  end
  ...
end

然后你会把它称为:

/app/sync/file_maker_sync.rb
class FileMakerSync
  def self.load_student(student_id)
    fm_student = Filemaker::FMStudent.find_single(student_id: student_id)
  end
  ...
end