RSpec 和 ActiveModel [英] RSpec and ActiveModel

查看:36
本文介绍了RSpec 和 ActiveModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 activemodel 的模块,我想使用 rspec 对其进行测试.

这是我目前的设置:

lib/|__ my_module/||__ base.rb|__ my_module.rb规格/|__ my_module_spec.rb|__ spec_helper.rb|__ 支持/|__ shared_examples/|__ active_model.rb

在my_class.rb"中:

需要active_model"需要my_module/base"

在base.rb"中:

模块 MyModule班级基地扩展 ActiveModel::Naming包括 ActiveModel::Conversion包括 ActiveModel::Validations结尾结尾

在my_module_spec.rb"中:

需要'spec_helper'描述 MyModule 做描述 MyModule::Base 做it_behaves_like "ActiveModel"结尾结尾

在spec_helper.rb"中:

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))$LOAD_PATH.unshift(File.dirname(__FILE__))需要'rspec'需要'my_module'# 需要带有自定义匹配器和宏等的支持文件,# 在 ./support/及其子目录中.目录["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f|需要 f}RSpec.configure 做 |config|结尾

在active_model.rb"里面:

# 取自 http://pivotallabs.com/users/jdean/blog/articles/1706-form-b​​acking-objects-for-fun-and-profitshared_examples_for "ActiveModel" 做需要测试/单元/断言"需要'active_model/lint'包括测试::单元::断言包括 ActiveModel::Lint::Tests在做之前@model = 主题结尾ActiveModel::Lint::Tests.public_instance_methods.map { |方法|method.to_s }.grep(/^test/).each do |method|示例(method.gsub('_', '')) { 发送方法 }结尾结尾

我认为我的设置没问题,但是当我尝试运行 bundle exec rspec spec 时出现以下错误:

失败:1) MyModule MyModule::Base 的行为类似于 ActiveModel 测试到关键失败/错误:示例(method.gsub('_','')){发送方法}2) MyModule MyModule::Base 的行为类似于 ActiveModel 测试到参数失败/错误:示例(method.gsub('_','')){发送方法}3) MyModule MyModule::Base 的行为类似于 ActiveModel 对部分路径的测试失败/错误:示例(method.gsub('_','')){发送方法}4) MyModule MyModule::Base 表现得像 ActiveModel 测试有效吗?失败/错误:示例(method.gsub('_','')){发送方法}5) MyModule MyModule::Base 表现得像 ActiveModel 测试一样吗?失败/错误:示例(method.gsub('_','')){发送方法}6) MyModule MyModule::Base 行为类似于 ActiveModel 测试模型命名失败/错误:示例(method.gsub('_','')){发送方法}7) MyModule MyModule::Base 表现得像 ActiveModel 测试错误 aref失败/错误:示例(method.gsub('_','')){发送方法}8) MyModule MyModule::Base 行为类似于 ActiveModel 测试错误完整消息失败/错误:示例(method.gsub('_','')){发送方法}

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

提前致谢

解决方案

您尝试做的太多了.因为您要测试 ActiveModel 库.你只需要测试你的对象是否包含这个模块

<预><代码>需要'spec_helper'描述 MyModule 做描述 MyModule::Base 做它 { MyModule::Base.should include(ActiveModel::Conversion) }它 { MyModule::Base.should include(ActiveModel::Validations) }结尾结尾

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

解决方案

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

这篇关于RSpec 和 ActiveModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆