Como pode criar o seguinte matcher RSpec?
foo.bars.should incude_at_least_one {|bar| bar.id == 42 }
Deixe-me saber se estou reinventando a roda, mas também estou curioso para saber como criar um correspondente personalizado que leva um bloco. Alguns dos matchers internos fazem isso, então é possível. Eu tentei isso:
RSpec::Matchers.define :incude_at_least_one do |expected|
match do |actual|
actual.each do |item|
return true if yield(item)
end
false
end
end
Eu também tentei passar &block
em ambas as folhas. Eu estou perdendo algo simples.
Respostas:
1 para resposta № 1Comecei com o código de Neil Slater e consegui que funcionasse:
class IncludeAtLeastOne
def initialize(&block)
@block = block
end
def matches?(actual)
@actual = actual
@actual.any? {|item| @block.call(item) }
end
def failure_message_for_should
"expected #{@actual.inspect} to include at least one matching item, but it did not"
end
def failure_message_for_should_not
"expected #{@actual.inspect} not to include at least one, but it did"
end
end
def include_at_least_one(&block)
IncludeAtLeastOne.new &block
end
0 para resposta № 2
Tem havido discussão sobre como adicionar um matcher ao rspec. Não tenho certeza sobre a sua pergunta de bloqueio, mas você poderia representar esse teste na aparência não tão elegante:
foo.bars.any?{|bar| bar.id == 42}.should be_true
Provavelmente mais fácil do que fazer um matcher personalizado e deve ser legível se o seu teste for algo como it "should include at least one foo matching the id"
0 para resposta № 3
O RSpec DSL não fez isso, mas você poderia fazer algo assim:
class IncludeAtLeastOne
def matches?(target)
@target = target
@target.any? do |item|
yield( item )
end
end
def failure_message_for_should
"expected #{@target.inspect} to include at least one thing"
end
def failure_message_for_should_not
"expected #{@target.inspect} not to include at least one"
end
end
def include_at_least_one
IncludeAtLeastOne.new
end
describe "foos" do
it "should contain something interesting" do
[1,2,3].should include_at_least_one { |x| x == 1 }
end
end