通过rspec测试

时间:2019-03-05 18:56:41

标签: ruby rspec

嘿,我正在以扫雷游戏作为我的项目,但在通过此测试时遇到了问题。

public static class LibraryConstants
{
    public const string DllName = "MyFile.dll";
}

public class OtherClass
{
    [DllImport(LibraryConstants.DllName, CharSet = CharSet.Unicode)]
    public static extern string someFunction(string directory);
}

这是我的简单方法:

  it "should initialise each position in each column in matrix as _" do
            matrix = []
            matrix[0] = ["_", "_", "_", "_", "_", "_","_"]
            matrix[1] = ["_", "_", "_", "_", "_", "_","_"]
            matrix[2] = ["_", "_", "_", "_", "_", "_","_"]
            matrix[3] = ["_", "_", "_", "_", "_", "_","_"]
            matrix[4] = ["_", "_", "_", "_", "_", "_","_"]
            matrix[5] = ["_", "_", "_", "_", "_", "_","_"]
            matrix[6] = ["_", "_", "_", "_", "_", "_","_"]
            @game.clearcolumns
            @game.matrix.should eql matrix
        end

由于某种原因,我失败了:

def clearcolumns
        def matrix
            matrix = []
            matrix[0] = ["_", "_", "_", "_", "_", "_","_"]
            matrix[1] = ["_", "_", "_", "_", "_", "_","_"]
            matrix[2] = ["_", "_", "_", "_", "_", "_","_"]
            matrix[3] = ["_", "_", "_", "_", "_", "_","_"]
            matrix[4] = ["_", "_", "_", "_", "_", "_","_"]
            matrix[5] = ["_", "_", "_", "_", "_", "_","_"]
            matrix[6] = ["_", "_", "_", "_", "_", "_","_"]
        end
    end

1 个答案:

答案 0 :(得分:3)

ruby​​中的方法(没有显式的return语句)将返回已执行的最后一行代码的结果。在您的情况下,matrix的最后一行是matrix[6] = ["_", "_", "_", "_", "_", "_","_"],因此该方法将始终返回["_", "_", "_", "_", "_", "_","_"]。如果要返回完整矩阵,则可以改为:

def matrix
  # Initialize the matrix variable
  matrix = []

  # Explicitly set each row of the matrix
  matrix[0] = ["_", "_", "_", "_", "_", "_","_"]
  matrix[1] = ["_", "_", "_", "_", "_", "_","_"]
  matrix[2] = ["_", "_", "_", "_", "_", "_","_"]
  matrix[3] = ["_", "_", "_", "_", "_", "_","_"]
  matrix[4] = ["_", "_", "_", "_", "_", "_","_"]
  matrix[5] = ["_", "_", "_", "_", "_", "_","_"]
  matrix[6] = ["_", "_", "_", "_", "_", "_","_"]

  # Return the matrix
  matrix
end