2011-06-13 55 views
1

我正在尝试RSpec与一点tic tac脚趾游戏。到目前为止,我有这个天赋两个应该在一个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 

有没有更好的办法做到这一点,利用其()方法?

回答

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 
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 
+0

我看到'empty?'就像一块空板,这个词超载,所以就把我扔了。 – jdeseno 2011-06-13 16:41:49

+0

是的。这是我想要的输出。如果没有别的办法,我会这样写。我只是喜欢它的()语法:p – pvinis 2011-06-13 17:27:21

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 

这是不是这样关于错误的信息,因为你不会区分哪一部分是错误的。

相关问题