首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迷你版回形针Shoulda Matcher

迷你版回形针Shoulda Matcher
EN

Stack Overflow用户
提问于 2013-05-03 12:12:31
回答 2查看 447关注 0票数 1

Paperclip Documentation提供了为RSpec和Test::Unit设置回形针的Shoulda匹配器的说明。然而,当我尝试为Minitest设置它们时,我没有成功(我遵循了与Test::Unit相同的说明)。

有谁知道什么需要使Papercips Shoulda匹配器与Minitest一起工作吗?

EN

回答 2

Stack Overflow用户

发布于 2014-11-04 02:12:23

如果有人像我一样从谷歌来到这里,did...this最终用minitest-spec-rails和一些自定义断言为我工作

代码语言:javascript
复制
# test_helper.rb

require 'paperclip/matchers'

class ActiveSupport::TestCase
  extend Paperclip::Shoulda::Matchers
  include Paperclip::Shoulda::Matchers

  # ... other code

end

module Minitest
  module Assertions
    ##
    # Passes if matcher.matches?(subject) returns true
    #
    # Example:
    #
    #   def test_validations
    #     assert_must be_valid, @user
    #   end
    def assert_must(matcher, subject, msg = nil)
      msg = message(msg) do
        if matcher.respond_to? :failure_message
          matcher.failure_message # RSpec 3.x, 1.1
        else
          matcher.failure_message_for_should # RSpec 2.x, 1.2
        end
      end

      assert matcher.matches?(subject), msg
    end

    ##
    # Facilitator to assert_must for use with minitest-spec. If no subject
    # given, defaults to matching against the current `subject` or the
    # instance variable `@subject`.
    #
    # Example:
    #
    #   subject { Order.new }
    #
    #   it "should have associations" do
    #     must belong_to :account
    #     must have_many :line_items
    #   end
    def must(matcher, subject = @subject || subject, msg = nil)
      assert_must matcher, subject, msg
    end

    ##
    # Passes if matcher.matches?(subject) returns false
    #
    # Example:
    #
    #   def test_validations
    #     assert_wont be_valid, @user
    #   end
    def assert_wont(matcher, subject, msg = nil)
      msg = message(msg) do
        if matcher.respond_to? :failure_message_when_negated # RSpec 3.x
          matcher.failure_message_when_negated # RSpec 3.x
        elsif matcher.respond_to? :failure_message_for_should_not
          matcher.failure_message_for_should_not # RSpec 2.x, 1.2
        else
          matcher.negative_failure_message # RSpec 1.1
        end
      end

      if matcher.respond_to? :does_not_match?
        assert matcher.does_not_match?(subject), msg
      else
        refute matcher.matches?(subject), msg
      end
    end

    ##
    # Facilitator to assert_wont for use with minitest-spec. If no subject
    # given, defaults to matching against the current `subject` or the
    # instance variable `@subject`.
    #
    # Example:
    #
    #   subject { User.new }
    #
    #   it "should validate" do
    #     wont have_valid(:email).when("foo", "foo@bar", "@bar.com")
    #   end
    def wont(matcher, subject = @subject || subject, msg = nil)
      assert_wont matcher, subject, msg
    end
  end
end
票数 1
EN

Stack Overflow用户

发布于 2013-11-07 21:20:56

我为此创建了一个gem:https://github.com/cschramm/rspec2minitest

只需将其包含在Gemfile中并将require 'rspec2minitest/paperclip'放入test_helper即可。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16351468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档