【问题标题】:Paperclip Shoulda Matchers for Miniteset用于 Miniset 的回形针应该匹配器
【发布时间】:2013-05-03 04:12:31
【问题描述】:

Paperclip Documentation 提供了为 RSpec 和 Test::Unit 设置 Paperclip 的 Shoulda 匹配器的说明。但是,当我尝试为 Minitest 设置它们时,我没有成功(我按照与 Test::Unit 相同的说明进行操作)。

有谁知道使 Papercips 应该匹配器与 Minitest 一起使用需要什么?

【问题讨论】:

    标签: ruby-on-rails ruby paperclip minitest shoulda


    【解决方案1】:

    如果有人像我一样从 Google 来到这里......这最终对我来说是 minitest-spec-rails 和一些自定义断言

    # 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
    

    【讨论】:

      【解决方案2】:

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

      只需将其包含在您的 Gemfile 中,然后将 require 'rspec2minitest/paperclip' 放入您的 test_helper 中即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-26
        相关资源
        最近更新 更多