Rspec根据哈希内容运行生成的测试

时间:2013-06-18 22:17:05

标签: ruby-on-rails ruby rspec

也许我现在不敢看,但我尝试根据哈希的内容运行动态规范。在这种特殊情况下,不同版本的电子邮件部分应包含相同的内容,如下面的分散:

context "testcontext" do
  before do
    #testsetup stuff
    @versions = {"Text-Version" => current_email.parts[0].body , "HTML-Version" => current_email.parts[1].body}
  end

  it "sets the correct subject" do
    current_email.subject.should =~ /subject_regex/
  end

  @versions.each do |key, body|

    it "test something in mail for #{key}" do
      body.should include("something i want to be there")
    end
  end
end

但是@versions是nil然后运行测试。我希望它是我在前一块中定义的值。

2 个答案:

答案 0 :(得分:0)

试试这个:

before(:all) do
  #testsetup stuff
  @versions = {"Text-Version" => current_email.parts[0].body , "HTML-Version" =>     current_email.parts[1].body}
end

答案 1 :(得分:0)

这个怎么样?

context "teh emailz" do
  @parts = [:text, :html]
  before do
    @versions = {:text => current_email.parts[0].body,
                :html => current_email.parts[1].body}
  end

  it "sets the correct subject" do
    current_email.subject.should =~ /subject_regex/
  end

  @parts.each do |name|
    it "test something the #{name} part of the body" do
      body = @versions[name]
      body.should include("something i want to be there")
    end
  end
end