相关疑难解决方法(0)

在rspec中测试模块

在rspec中测试模块的最佳实践是什么?我有一些模块包含在少数模型中,现在我只是对每个模型进行了重复测试(差异很小).有没有办法让它干涸?

ruby unit-testing rspec

170
推荐指数
10
解决办法
7万
查看次数

Rails和RSpec - 测试关注类方法

我有以下(简化)Rails关注:

module HasTerms
  extend ActiveSupport::Concern

  module ClassMethods
    def optional_agreement
      # Attributes
      #----------------------------------------------------------------------------
      attr_accessible :agrees_to_terms
    end

    def required_agreement
      # Attributes
      #----------------------------------------------------------------------------
      attr_accessible :agrees_to_terms

      # Validations
      #----------------------------------------------------------------------------
      validates :agrees_to_terms, :acceptance => true, :allow_nil => :false, :on => :create
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我无法想出在RSpec中测试此模块的好方法 - 如果我只是创建一个虚拟类,当我尝试检查验证是否正常时,我会收到活动记录错误.还有其他人遇到过这个问题吗?

rspec module ruby-on-rails ruby-on-rails-3

22
推荐指数
3
解决办法
2万
查看次数

测试使用ActiveRecord的问题/模块

情景 我已经提到了一个叫做的问题Taggable.它是一个允许任何模型支持标记的模块.我已经包含了这种担忧/模块到像模特User,Location,Places,Projects.

我想为这个模块编写测试,但不知道从哪里开始.

问题
1.我可以对此Taggable问题进行隔离测试吗?
在下面的示例中,测试失败,因为测试正在寻找a dummy_class table.我假设它正在这样做因为has_many代码,Taggable因此它期望'DummyClass'是一个ActiveRecord对象.

# /app/models/concerns/taggable.rb
module Taggable
  extend ActiveSupport::Concern

  included do
    has_many :taggings, :as => :taggable, :dependent=> :destroy
    has_many :tags, :through => :taggings
  end

  def tag(name)
    name.strip!
    tag = Tag.find_or_create_by_name(name)
    self.taggings.find_or_create_by_tag_id(tag.id)
  end
end


# /test/models/concerns/taggable_test.rb
require 'test_helpers'

class DummyClass
end

describe Taggable do
  before do
    @dummy = DummyClass.new
    @dummy.extend(Taggable)
  end

  it …
Run Code Online (Sandbox Code Playgroud)

ruby unit-testing ruby-on-rails activesupport minitest

14
推荐指数
1
解决办法
5585
查看次数