【发布时间】:2023-03-07 21:18:01
【问题描述】:
我正在用 ruby 制作康威生命游戏。这是我的 cell_spec.rb 文件。我在第 10 行遇到了失败/错误:
expect(GameOfLife::Cell.new_dead_cell).to be_dead
我有另一个文件cell.rb,其中定义了类单元格。如何在这个文件中实现自定义谓词数学?
require 'spec_helper'
describe GameOfLife::Cell do
def build_neighbours(live_count)
[GameOfLife::Cell.new_live_cell] * live_count +
[GameOfLife::Cell.new_dead_cell] * (8 - live_count)
end
context "factory" do
it "can be dead" do
expect(GameOfLife::Cell.new_dead_cell).to be_dead
end
it "can be alive" do
expect(GameOfLife::Cell.new_live_cell).to be_alive
end
end
context "live cell generation rules" do
let(:cell) { GameOfLife::Cell.new_live_cell }
[0, 1].each do |i|
it "dies when there are #{i} live neighbours" do
expect(cell.next_generation(build_neighbours(i))).to be_dead
end
end
[2, 3].each do |i|
it "lives when there are #{i} live neighbours" do
expect(cell.next_generation(build_neighbours(i))).to be_alive
end
end
(4..8).each do |i|
it "dead when there are #{i} live neighbours" do
expect(cell.next_generation(build_neighbours(i))).to be_dead
end
end
end
context "dead cell generation rules" do
let(:cell) { GameOfLife::Cell.new_dead_cell }
(0..2).each do |i|
it "dies when there are #{i} live neighbours" do
expect(cell.next_generation(build_neighbours(i))).to be_dead
end
end
[3].each do |i|
it "lives when there are #{i} live neighbours" do
expect(cell.next_generation(build_neighbours(i))).to be_alive
end
end
(4..8).each do |i|
it "dead when there are #{i} live neighbours" do
expect(cell.next_generation(build_neighbours(i))).to be_dead
end
end
end
end
这是我的具有单元类的 cell.rb 文件。我想知道死亡代码的实现?还活着?方法。请帮帮我
class GameOfLife::Cell
ALIVE = "alive"
DEAD = "dead"
# lost implementation
def self.new_dead_cell
return DEAD
end
def self.new_live_cell
return ALIVE
end
def dead?
end
def alive?
end
end
【问题讨论】:
-
请将完整的错误信息添加到您的问题中。
-
1) GameOfLife::Cell factory can be dead 失败/错误:expect(GameOfLife::Cell.new_dead_cell).to be_dead 预期死去响应
dead?# ./spec/game_of_life/cell_spec .rb:10:inblock (3 levels) in <top (required)>' 2) GameOfLife::Cell factory can be alive Failure/Error: expect(GameOfLife::Cell.new_live_cell).to be_alive expected alive to respond toalive?` # ./spec/game_of_life/cell_spec.rb:14:in `block (3 levels) in' -
它几乎可以告诉您代码有什么问题。为了使
be_dead匹配器工作,你需要在GameOfLife::Cell类中实现dead?方法。 -
我已经实现死了?方法..但是这个错误是“预期死亡响应死亡?”..这个问题将如何解决def dead?死胡同
-
new_dead_cell方法返回了什么?它应该返回实例,所以最后一行应该是self,除非方法中的最后一条语句已经返回了实例。