两个应该在一个rspec块中

时间:2011-06-13 15:52:51

标签: ruby rspec

我正在用一个小小的tic tac toe游戏尝试RSpec。到目前为止我有这个规范

require './tic_tac_toe'

describe TicTacToe do
  subject { TicTacToe.new }

  context "when starting a new game" do
    its(:grid) { should have(9).cells }
    its(:grid) { should be_empty }
  end
end

这个工作得很好,但输出是这样的(网格每次显示一次测试两次) 我希望它能在一个网格下显示两个测试。

TicTacToe
  when starting a new game
    grid
      should have 9 cells
    grid
      should be empty
我可以写这样的东西吗?

its(:grid) { should have(9).cells and should be_empty }

或类似的东西?

its(:grid) { should have(9).cells and its(:cells) { should be_empty} }

谢谢!


编辑:

I did what I want using this 
context "when starting a new game" do
    describe "grid" do
      subject { @game.grid }
      it "should have 9 empty cells" do
        should have(9).cells
        should be_empty
      end
    end
  end

使用its()方法有更好的方法吗?

3 个答案:

答案 0 :(得分:1)

its相当于describeit,所以我不这么认为。您可以这样明确地写出来:

describe TicTacToe do
  subject { TicTacToe.new.grid }

  context "when starting a new game" do
    describe "grid" do
      it { should have(9).cells}
      it { should be_empty}
    end
  end
end

我对规格感到有些困惑,它有9个单元格,也是空的?所以我不确定这是你想要的,但输出将是:

TicTacToe
  when starting a new game
    grid
      should have 9 cells
      should be empty

答案 1 :(得分:0)

这是做我想要的一种方式..不使用它的(),但它是我想要的输出。

context "when starting a new game" do
    describe "grid" do
      subject { @game.grid }
      it "should have 9 empty cells" do
        should have(9).cells
        should be_empty
      end
    end
  end

答案 2 :(得分:0)

你可以,但我建议你不应该这就是为什么:

目前:

context "when starting a new game" do
  its(:grid) { should have(9).cells }
  its(:grid) { should be_empty }
end

将检查有9个单元格的网格并报告。 然后它将分别查看网格是否为空。

这将在以下条件下正确报告:

A grid with 9 elements that is empty with => true, true
A grid with 8 elements that is empty with => false,true
A grid with 9 elements that is not empty with => true, false
A grid with 8 elements that is not empty with => false, false

但是,如果您将条件放在一起,那么对于上述情况,您将获得单一回报,例如

=> true
=> false
=> false
=> false

这对于谬误并不是那么有用,因为你不会区分哪一部分是假的。