0

テストの方法でいくつかのキュウリの概念を使用していますが、キュウリがまったく好きではなかったため、Steak を使用して受け入れテストを行っています。私はテスト用の宣言型と命令型のスタイルが好きで、いくつかの期待を精巧なカスタム rspec マッチャーに抽象化しています。このマッチャーは、マッチ メソッド内で他のマッチャーを使用します。例を次に示します。

RSpec::Matchers.define :show_post do |post|
  match do |page|
    within '.post' do
      page.should have_content post.title
      page.should have_content post.tagline
      page.should have_content post.body
      page.should list_author  post.author
    end
  end
end

私が抱えている唯一の問題は、マッチャーが失敗した場合、何が欠けているかについての洞察を与えない一般的なメッセージを受け取ることです. .

私は次のことができるという表現力が本当に好きなので、しばらくの間、この煩わしさと一緒に暮らしてきました。

page.should show_post Post.last
4

2 に答える 2

0

さらに良いことに:

module DefineMatcher
  def define_matcher name, &block
    klass = Class.new do
      include Capybara::RSpecMatchers

      attr_reader :expected

      def initialize expected
        @expected = expected
      end

      define_method :matches?, &block
    end

    define_method name do |expected|
      klass.new expected
    end
  end
end


module PageMatchers
  extend DefineMatcher

  define_matcher :show_notice do |page|
    within '.alert-notice' do
      page.should have_content expected
    end
  end
end

RSpec.configuration.include PageMatchers, :type => :acceptance
于 2013-01-25T04:22:51.407 に答える
0

とった:

class ShowsPost
  include Capybara::RSpecMatchers

  def initialize post
    @post = post
  end

  def matches? page
    page.should have_content @post.title
    page.should have_content @post.tagline
    page.should have_content @post.body
    page.should list_author  @post.author
  end
end

def show_post post
  ShowsPost.new(post)
end
于 2012-09-05T00:55:23.533 に答える