0

我有

last_email_sent.body.should include "Company Name"
last_email_sent.body.should include "SomeCompany"
last_email_sent.body.should include "Email"
last_email_sent.body.should include "test@test.pl"

我想用数组替换它

last_email_sent.body.should include ["test@test.pl", "Email"]
4

4 回答 4

3

你可以简单地循环:

["test@test.pl", "Email"].each { |str| last_email_sent.body.should include str }

或者,如果您喜欢匹配器语法,请编写自己的匹配器:

RSpec::Matchers.define :include_all do |include_items|
  match do |given|
    @errors = include_items.reject { |item| given.include?(item) }
    @errors.empty?
  end

  failure_message_for_should do |given|
    "did not include \"#{@errors.join('\", \"')}\""
  end

  failure_message_for_should_not do |given|
     "everything was included"
  end

  description do |given|
    "includes all of #{include_items.join(', ')}"
  end
end

像这样调用它:

last_email_sent.body.should include_all ["test@test.pl", "Email"]
于 2013-04-16T09:36:01.513 回答
1

试试这个

["test@test.pl", "Email"].all?{ |str| last_email_sent.body[str] }.should == true
于 2013-04-16T09:26:57.333 回答
1

我喜欢将这样的数组放在实用程序方法中:

规范/支持/实用程序.rb

def email_body_elements
  ["Company Name", "Some Company", "Email", "test@test.pl"]
end

规格/your_spec.rb

email_body_elements.each do |element|
  last_email_sent.body.should include element
end
于 2013-04-16T09:36:45.953 回答
0

如果last_email_sent.body恰好是Company Name SomeCompany test@test.pl Email

last_email_sent.body.should include ["Company Name", "SomeCompany", "test@test.pl", "Email"].join(" ")
于 2013-04-16T09:27:43.773 回答