我刚开始写RSpec的测试,我碰上了thoughtbot的风格指南,其中建议对let,let!,before和subject(等等).
我也在其他几个地方读过类似的建议(包括旧的RSpec文档警告before(:all)),但我似乎无法找到反对它们的实际论据.
所以问题是:
为什么我不应该在测试中使用这些方法?什么是更好的方法?
我正在尝试在 Rails 4.2.4 应用程序中为 RSpec + Sidekiq 编写一些规范,但遇到了一些问题。
我的代码如下所示:
class MyImportJob
include Sidekiq::Worker
sidekiq_options queue: :default
def perform(params)
# Do magic
end
end
Run Code Online (Sandbox Code Playgroud)
和规范:
describe MyImportJob, type: :job do
let(:panel) { create(:panel) }
describe '#perform' do
context 'unsuccessfully' do
it 'raises ArgumentError if no panel param was passed' do
expect {subject.perform_async()}.to raise_error(ArgumentError)
end
end
context 'successfully' do
it 'given a panel, it increases the job number' do
expect {
subject.perform_async(panel_id: panel.id)
}.to change(subject.jobs, :size).by(1)
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
Failure/Error: …Run Code Online (Sandbox Code Playgroud)