RSpec and ActiveModel

可紊 提交于 2019-12-10 22:35:37

问题


I have a module and it includes activemodel and I want to test it using rspec.

This is my setup so far:

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

inside 'my_class.rb':

require "active_model"
require "my_module/base"

inside 'base.rb':

module MyModule
  class Base
    extend ActiveModel::Naming

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

inside 'my_module_spec.rb':

require 'spec_helper'

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

inside '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

inside '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

I thought my setup is alright but when I tried to run bundle exec rspec spec I am getting these errors:

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 }

What am I doing wrong here? How do I test ActiveModel properly?

Thanks in advance


回答1:


You try to do too much. Because you want test the ActiveModel library. You just need test if your Object include this module


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


来源:https://stackoverflow.com/questions/9131476/rspec-and-activemodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!