重构RSpec测试,使其在文件顶部只有一个require(仅限Ruby,无Rails)
我正在为我制作的Ruby tic tac toe游戏编写RSpec测试。测试在最初失败后通过,所以这是一个好迹象。这是computer_spec.rb文件:重构RSpec测试,使其在文件顶部只有一个require(仅限Ruby,无Rails),ruby,rspec,refactoring,tdd,Ruby,Rspec,Refactoring,Tdd,我正在为我制作的Ruby tic tac toe游戏编写RSpec测试。测试在最初失败后通过,所以这是一个好迹象。这是computer_spec.rb文件: require 'computer' require 'board' describe "the computer selects the winning move as spot 3" do it "when spots 1 and 2 are chosen" do computer = Computer.new b
require 'computer'
require 'board'
describe "the computer selects the winning move as spot 3" do
it "when spots 1 and 2 are chosen" do
computer = Computer.new
board = Board.new
computer.winning_move(board, [1,2]).should eq(3)
end
end
现在,因为它是一个computer_spec.rb文件,所以它实际上应该只在这个文件的顶部有require'computer'。我的意思是,它肯定能工作,但我想成为RSpec的高手,用最好、最简洁的方式做事
当然,计算机类在获胜移动方法中使用board.rb。胜诉通过了两个论点,董事会和计算机。电路板本身是一个散列(tile是键,X或O是值)。这是一点代码,但您可以在此处看到board.rb:
我试图将散列硬编码到computer_spec.rb,但没有成功。其中一个比较常见的错误是“未初始化常量板”,我认为这是有道理的
那么,在这个文件的顶部只需要“计算机”是最好的方法是什么呢
感谢您的任何意见
编辑:我刚刚使用了spec_helper.rb,不确定为什么我从一开始就没有使用它。一些选项:
- 将计算机和电路板类定义放在一个文件中,并包括该文件
- 将线路板添加为自动加载的文件
- 添加ActiveSupport::Dependencies.autoload\u路径