RSpec和ActiveModel

时间:2012-02-03 15:52:25

标签: ruby-on-rails rspec activemodel

我有一个模块,它包含activemodel,我想用rspec测试它。

这是我到目前为止的设置:

lib/
 |__ my_module/
 |    |__ base.rb 
 |__ my_module.rb
spec/
 |__ my_module_spec.rb
 |__ spec_helper.rb
 |__ support/
      |__ shared_examples/
           |__ active_model.rb

在'my_class.rb'中:

require "active_model"
require "my_module/base"

在'base.rb'里面:

module MyModule
  class Base
    extend ActiveModel::Naming

    include ActiveModel::Conversion
    include ActiveModel::Validations
  end
end

在'my_module_spec.rb'中:

require 'spec_helper'

describe MyModule do
  describe MyModule::Base do
    it_behaves_like "ActiveModel"
  end
end

在'spec_helper.rb'里面:

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
require 'my_module'

# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

RSpec.configure do |config|
end

在'active_model.rb'中:

# taken from http://pivotallabs.com/users/jdean/blog/articles/1706-form-backing-objects-for-fun-and-profit

shared_examples_for "ActiveModel" do
  require 'test/unit/assertions'
  require 'active_model/lint'
  include Test::Unit::Assertions
  include ActiveModel::Lint::Tests

  before do
    @model = subject
  end

  ActiveModel::Lint::Tests.public_instance_methods.map { |method| method.to_s }.grep(/^test/).each do |method|
    example(method.gsub('_', ' ')) { send method }
  end
end

我认为我的设置没问题,但当我尝试运行bundle exec rspec spec时,我收到了这些错误:

Failures:

  1) MyModule MyModule::Base behaves like ActiveModel test to key
     Failure/Error: example(method.gsub('_', ' ')) { send method }

  2) MyModule MyModule::Base behaves like ActiveModel test to param
     Failure/Error: example(method.gsub('_', ' ')) { send method }

  3) MyModule MyModule::Base behaves like ActiveModel test to partial path
     Failure/Error: example(method.gsub('_', ' ')) { send method }

  4) MyModule MyModule::Base behaves like ActiveModel test valid?
     Failure/Error: example(method.gsub('_', ' ')) { send method }

  5) MyModule MyModule::Base behaves like ActiveModel test persisted?
     Failure/Error: example(method.gsub('_', ' ')) { send method }

  6) MyModule MyModule::Base behaves like ActiveModel test model naming
     Failure/Error: example(method.gsub('_', ' ')) { send method }

  7) MyModule MyModule::Base behaves like ActiveModel test errors aref
     Failure/Error: example(method.gsub('_', ' ')) { send method }

  8) MyModule MyModule::Base behaves like ActiveModel test errors full messages
     Failure/Error: example(method.gsub('_', ' ')) { send method }

我在这里做错了什么?如何正确测试ActiveModel?

提前致谢

1 个答案:

答案 0 :(得分:0)

你试着做太多。因为您想要测试ActiveModel库。如果您的对象包含此模块,您只需要测试


require 'spec_helper'

describe MyModule do
  describe MyModule::Base do
    it { MyModule::Base.should include(ActiveModel::Conversion) }
    it { MyModule::Base.should include(ActiveModel::Validations) }
  end
end