2012-09-04 18 views
5

मैं अपने परीक्षण में एक पंक्ति है:Minitest कल्पना कस्टम मिलान

page.has_reply?("my reply").must_equal true 

और इसे और अधिक पठनीय बनाने के लिए मैं एक कस्टम मिलान का उपयोग करना चाहते:

page.must_have_reply "my reply" 

https://github.com/zenspider/minitest-matchers मैं के लिये दस्तावेज के आधार पर उम्मीद है कि मुझे ऐसा मैचर लिखना होगा जो इस तरह कुछ दिखता है:

def have_reply(text) 
    subject.has_css?('.comment_body', :text => text) 
end 
MiniTest::Unit::TestCase.register_matcher :have_reply, :have_reply 

समस्या यह है कि मैं नहीं देख सकता विषय का संदर्भ प्राप्त करने के लिए (यानी। पेज ऑब्जेक्ट)। दस्तावेज़ कहते हैं, "नोट विषय दावा में पहला तर्क होना चाहिए" लेकिन यह वास्तव में मदद नहीं करता है।

उत्तर

6

एक छोटा सा उदाहरण है, आप एक वर्ग बना सकते हैं जो विधियों matches?, failure_message_for_should, failure_message_for_should_not के तरीकों का सेट करना चाहिए। matches? विधि में आप इस विषय का संदर्भ प्राप्त कर सकते हैं।

class MyMatcher 
    def initialize(text) 
    @text = text 
    end 

    def matches? subject 
    subject =~ /^#{@text}.*/ 
    end 

    def failure_message_for_should 
    "expected to start with #{@text}" 
    end 

    def failure_message_for_should_not 
    "expected not to start with #{@text}" 
    end 
end 

def start_with(text) 
    MyMatcher.new(text) 
end 
MiniTest::Unit::TestCase.register_matcher :start_with, :start_with 

describe 'something' do 
    it 'must start with...' do 
    page = 'my reply' 
    page.must_start_with 'my reply' 
    page.must_start_with 'my ' 
    end 
end 
1

आप यहां क्या चाहते हैं पाने के कई तरीके हैं। सबसे आसान तरीका है कि दावे, अपेक्षाओं या मैचर्स के साथ गड़बड़ न करें और केवल एक जोर दें। तो, आप पहले से ही यह सोचते हैं has_reply? विधि परिभाषित किया गया है, तो आप सिर्फ यह इस्तेमाल कर सकते हैं:

assert page.has_reply?("my reply") 

लेकिन, यह है कि आप must_have_reply वाक्य रचना आप के लिए पूछ रहे हैं नहीं मिलता है। और मुझे संदेह है कि आपके पास वास्तव में has_reply? विधि है। चलिए, शुरू करते हैं।

आपने पूछा "इस विषय का संदर्भ कैसे प्राप्त करें (यानी पृष्ठ वस्तु)"। इस मामले में विषय वस्तु है कि must_have_reply विधि को परिभाषित किया गया है। इसलिए, आपको subject के बजाय this का उपयोग करना चाहिए। लेकिन यह सब कुछ के रूप में सीधा नहीं है। Matchers एक संकेत का स्तर जोड़ते हैं कि हमारे पास सामान्य Assertions (assert_equal, refute_equal) या अपेक्षाओं (must_be_equal, wont_be_equal) के साथ नहीं है। यदि आप एक मचर लिखना चाहते हैं तो आपको Matcher API को लागू करने की आवश्यकता है।

सौभाग्य से आपके लिए आपको वास्तव में एपीआई को लागू नहीं करना है। चूंकि ऐसा लगता है कि आप पहले ही कैबबारा के have_css मैचर पर भरोसा करने का इरादा रखते हैं, हम बस कैपिबरा के हैसइलेक्टर क्लास का उपयोग कर सकते हैं और इसे उचित API लागू कर सकते हैं। हमें बस अपने स्वयं के मैचर्स मॉड्यूल को एक विधि के साथ बनाने की आवश्यकता है जो एक हैसइलेक्टर ऑब्जेक्ट देता है।

# Require Minitest Matchers to make this all work 
require "minitest/matchers" 
# Require Capybara's matchers so you can use them 
require "capybara/rspec/matchers" 

# Create your own matchers module 
module YourApp 
    module Matchers 
    def have_reply text 
     # Return a properly configured HaveSelector instance 
     Capybara::RSpecMatchers::HaveSelector.new(:css, ".comment_body", :text => text) 
    end 

    # Register module using minitest-matcher syntax 
    def self.included base 
     instance_methods.each do |name| 
     base.register_matcher name, name 
     end 
    end 
    end 
end 

फिर, अपने minitest_helper.rb फ़ाइल में, आप अपने matchers मॉड्यूल ताकि आप इसे उपयोग कर सकते हैं शामिल कर सकते हैं। (इस कोड में सभी परीक्षणों में मैचर शामिल होगा।)

class MiniTest::Rails::ActiveSupport::TestCase 
    # Include your module in the test case 
    include YourApp::Matchers 
end 

मिनीटेस्ट मैचर्स सभी कड़ी मेहनत करते हैं।अब आप एक अभिकथन के रूप में अपने मिलान का उपयोग कर सकते कर सकते हैं:

def test_using_an_assertion 
    visit root_path 
    assert_have_reply page, "my reply" 
end 

या, आप एक उम्मीद के रूप में अपने मिलान का उपयोग कर सकते हैं:

it "is an expectation" do 
    visit root_path 
    page.must_have_reply "my reply" 
end 

और अंत में आप एक विषय के साथ इसका इस्तेमाल कर सकते हैं:

describe "with a subject" do 
    before { visit root_path } 
    subject { page } 

    it { must have_reply("my reply") } 
    must { have_reply "my reply" } 
end 

महत्वपूर्ण: इसके लिए काम करने के लिए, आपको 'मणि मिनीटेस्ट-मैचर्स', '> = 1.2.0' का उपयोग करना होगा क्योंकि register_matcher को उस ge के पुराने संस्करणों में परिभाषित नहीं किया गया है मीटर।