为什么 RSpec 测试放在 `module` 关键字中?
Why are the RSpec tests placed within the `module` keyword?
首先,我不是要测试模块。我正在单个模块中测试 类。 This SO question/answer 没有描述我正在查看的代码,因此很难与我的好奇心相提并论。
我正在关注 with this tutorial,它已经过时了;但是,这是建议的资源。
我的具体问题是:
为什么规范文件中的 RSpec 语言放在 module TicTacToe
中,像这样:
require 'some_file'
module TicTacToe
#RSpec examples here
end
我对 RSpec 和一般编码还很陌生,所以我不熟悉 RSpec 中编写测试的惯例;这个组织有必要吗?过时?我的测试通过了,一切都按预期进行;我只是想学习如何编写测试,大多数规范以 RSpec.describe Foo do
或 describe Bar do
开头
我还没有看到 module
在 spec 文件中这样使用。我怀疑这是一个非常古老的教程。 (WordPress 有用地显示它是从 10 月 25 日开始的,但忽略了提及哪一年。但最早的评论是从 2014 年开始的)
我建议你找一个更新的教程。
describe
关键字在这里更合适。几年前,我使用的大部分规范看起来像这样:
RSpec.describe TicTacToe::SomeClass, type: :model do
#RSpec examples here
end
或者更现代,rspec 从规范文件的路径中识别类型(模型、控制器等):
# spec/model/tic_tac_toe/cell.rb
describe Cell do
let(:first_cell) { described_class.new }
#RSpec examples here
end
首先,我不是要测试模块。我正在单个模块中测试 类。 This SO question/answer 没有描述我正在查看的代码,因此很难与我的好奇心相提并论。
我正在关注 with this tutorial,它已经过时了;但是,这是建议的资源。
我的具体问题是:
为什么规范文件中的 RSpec 语言放在 module TicTacToe
中,像这样:
require 'some_file'
module TicTacToe
#RSpec examples here
end
我对 RSpec 和一般编码还很陌生,所以我不熟悉 RSpec 中编写测试的惯例;这个组织有必要吗?过时?我的测试通过了,一切都按预期进行;我只是想学习如何编写测试,大多数规范以 RSpec.describe Foo do
或 describe Bar do
我还没有看到 module
在 spec 文件中这样使用。我怀疑这是一个非常古老的教程。 (WordPress 有用地显示它是从 10 月 25 日开始的,但忽略了提及哪一年。但最早的评论是从 2014 年开始的)
我建议你找一个更新的教程。
describe
关键字在这里更合适。几年前,我使用的大部分规范看起来像这样:
RSpec.describe TicTacToe::SomeClass, type: :model do
#RSpec examples here
end
或者更现代,rspec 从规范文件的路径中识别类型(模型、控制器等):
# spec/model/tic_tac_toe/cell.rb
describe Cell do
let(:first_cell) { described_class.new }
#RSpec examples here
end