2012-09-04 5 views
5

私は私のテストでの行があります。Minitestスペックカスタム照合

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

、それをより読みやすくするために、私は、カスタム照合を使用したい:

page.must_have_reply "my reply" 

https://github.com/zenspider/minitest-matchers Iのためのドキュメントをもとにし私は次のようなものを書かなければならないと思っています:

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_shouldfailure_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

ここで必要なものを得る方法はたくさんあります。最も簡単な方法は、アサーション、期待、またはマッチャーをまったく混乱させず、ただアサーションを使用することです。あなたが求めているmust_have_reply構文を取得していないこと、

assert page.has_reply?("my reply") 

しかし:だから、あなたはすでに定義has_reply?方法を持っていると仮定すると、あなたはこれを使用することができます。そして、あなたは本当にhas_reply?メソッドを持っているのか疑問です。だから、始めましょう。

あなたの質問には「被写体(ページオブジェクト)への参照を取得する方法」があります。この場合、サブジェクトはメソッドが定義されているオブジェクトです。したがって、subjectの代わりにthisを使用する必要があります。しかしそれはすべてのものほど単純ではありません。 Matcherは、通常のアサーション(assert_equal,refute_equal)または期待値(must_be_equal,wont_be_equal)でない間接レベルを追加します。 Matcherを記述する場合は、Matcher APIを実装する必要があります。

幸運なことに、あなたは本当にAPIを実装する必要はありません。 Cabybaraのhave_cssマッチャーに頼っているようだから、CapybaraのHaveSelectorクラスを使い、適切なAPIを実装するだけです。私たちは、HaveSelectorオブジェクトを返すメソッドで独自のMatchersモジュールを作成するだけです。あなたがそれを使用できるように

# 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ファイルで、あなたのマッチャーモジュールを含めることができます。 (このコードでは、すべてのテストでマッチャーが使用されます)。

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

Minitest Matchersはすべてのハードリフトを行います。あなたは今、あなたは主張として、あなたのマッチャーを使用することができますすることができます

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 

重要:この機能を使用するには、register_matcherが以前のバージョンのgeで定義されていないため、 'gem minitest-matchers'、 '> = 1.2.0' m。