Created
June 26, 2012 17:23
-
-
Save rodrigomaia/2997223 to your computer and use it in GitHub Desktop.
How to create a RSpec Custom Matcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Matcher included_in | |
| #my test | |
| it "quando sortear, gravar os vencedores" do | |
| concorrentes = Concorrente.where(:sorteio_id => @sorteio.id) | |
| @sorteio.sortear | |
| @sorteio.vencedores.should included_in(concorrentes) | |
| end | |
| #spec/support/spec_array_helper.rb | |
| RSpec::Matchers.define :included_in do |expected| | |
| match do |actual| | |
| retorno = true | |
| retorno = false if actual.empty? || expected.empty? | |
| actual.each do |iten| | |
| if not expected.map(&:id).include?(iten.try(:id)) | |
| retorno = false | |
| break | |
| end | |
| end | |
| retorno | |
| end | |
| failure_message_for_should do |actual| | |
| "expected that #{actual} be included on #{expected}" | |
| end | |
| failure_message_for_should_not do |actual| | |
| "expected that #{actual} would not be included on #{expected}" | |
| end | |
| description do | |
| "be included on #{expected}" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment