【问题标题】:Rails Rspec let with variableRails Rspec 让变量
【发布时间】:2014-02-05 01:12:04
【问题描述】:

这是我的场景:

我有一堆 rspec 测试

describe "a bunch of tests" do
   let(:my_variable) {something}
   context "10 tests" do
      ...
   end
   context "another 10 tests" do
     ...
   end
end

我想将 my_variable 设置为别的并进行相同的 20 次测试,我可以再次这样做

describe "a bunch of tests" do
   let(:my_variable) {something else}
   context "10 tests" do
      ...
   end
   context "another 10 tests" do
     ...
   end
end

我想知道有没有更好的方法来做没有那么多重复的测试?

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby rspec ruby-on-rails-3.2


    【解决方案1】:

    你可以循环关闭它们:

    [something, something else].each do |value|
      describe "a bunch of tests" do
         let(:my_variable) { value }
         context "10 tests" do
            ...
         end
         context "another 10 tests" do
           ...
         end
      end
    end
    

    【讨论】:

    • 它就像一个魅力!你摇滚!我会尽快接受你的回答
    猜你喜欢
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多