如何使用 RSpec 测试带有参数的方法?
How to test a method with a parameter using RSpec?
我有以下 RSpec:
describe Job do
let(:job) { Job.new }
describe 'run_job' do
context 'success' do
my_param = [{"job1" => "run"}, {"job2" => "run again"}]
it 'should pass' do
test_result = job.run_job(my_param)
expect(test_result[0]["job1"]).to eq("run")
end
end
end
end
方法:
Class Job
def run_job(my_param)
# puts "#{my_param}"
my_param.each do|f|
# do something
end
end
end
当我 运行 测试时,我收到以下错误
NoMethodError:
undefined method `each' for nil:NilClass
我在控制台中打印出 my_param
并看到传递给测试的同一对象 [{"job1" => "run"}, {"job2" => "run again"}]
。我不知道为什么 my_param
在调用 .each
时是 nil
。我做错了什么?任何见解表示赞赏。
一种方法是在 before 块中定义并将其形成实例变量;
before { @my_param = [{"job1" => "run"}, {"job2" => "run again"}] }
或
但最好的方法是使用 let:
describe Job do
let(:job) { Job.new }
let(:my_param) { [{"job1" => "run"}, {"job2" => "run again"}] }
describe 'run_job' do
context 'success' do
it 'should pass' do
test_result = job.run_job(my_param)
expect(test_result[0]["job1"]).to eq("run")
end
end
end
end
my_param
应该在 it
块内定义,或者您应该使用 let
来定义 my_param
里面块
describe Job do
let(:job) { Job.new }
describe 'run_job' do
context 'success' do
it 'should pass' do
my_param = [{"job1" => "run"}, {"job2" => "run again"}]
test_result = job.run_job(my_param)
expect(test_result[0]["job1"]).to eq("run")
end
end
end
end
使用 let
describe Job do
let(:job) { Job.new }
describe 'run_job' do
context 'success' do
let(:my_param) { [{"job1" => "run"}, {"job2" => "run again"}] }
it 'should pass' do
test_result = job.run_job(my_param)
expect(test_result[0]["job1"]).to eq("run")
end
end
end
end
块前使用
describe Job do
let(:job) { Job.new }
describe 'run_job' do
context 'success' do
before(:all) do
@my_param = [{"job1" => "run"}, {"job2" => "run again"}]
end
it 'should pass' do
test_result = job.run_job(@my_param)
expect(test_result[0]["job1"]).to eq("run")
end
end
end
end
Better Specs推荐使用let
赋值变量:
describe Job do
let(:job) { Job.new }
describe 'run_job' do
context 'success' do
let(:my_param) { [{"job1" => "run"}, {"job2" => "run again"}] }
it 'should pass' do
test_result = job.run_job(my_param)
expect(test_result[0]["job1"]).to eq("run")
end
end
end
end
我有以下 RSpec:
describe Job do
let(:job) { Job.new }
describe 'run_job' do
context 'success' do
my_param = [{"job1" => "run"}, {"job2" => "run again"}]
it 'should pass' do
test_result = job.run_job(my_param)
expect(test_result[0]["job1"]).to eq("run")
end
end
end
end
方法:
Class Job
def run_job(my_param)
# puts "#{my_param}"
my_param.each do|f|
# do something
end
end
end
当我 运行 测试时,我收到以下错误
NoMethodError:
undefined method `each' for nil:NilClass
我在控制台中打印出 my_param
并看到传递给测试的同一对象 [{"job1" => "run"}, {"job2" => "run again"}]
。我不知道为什么 my_param
在调用 .each
时是 nil
。我做错了什么?任何见解表示赞赏。
一种方法是在 before 块中定义并将其形成实例变量;
before { @my_param = [{"job1" => "run"}, {"job2" => "run again"}] }
或
但最好的方法是使用 let:
describe Job do
let(:job) { Job.new }
let(:my_param) { [{"job1" => "run"}, {"job2" => "run again"}] }
describe 'run_job' do
context 'success' do
it 'should pass' do
test_result = job.run_job(my_param)
expect(test_result[0]["job1"]).to eq("run")
end
end
end
end
my_param
应该在 it
块内定义,或者您应该使用 let
来定义 my_param
里面块
describe Job do
let(:job) { Job.new }
describe 'run_job' do
context 'success' do
it 'should pass' do
my_param = [{"job1" => "run"}, {"job2" => "run again"}]
test_result = job.run_job(my_param)
expect(test_result[0]["job1"]).to eq("run")
end
end
end
end
使用 let
describe Job do
let(:job) { Job.new }
describe 'run_job' do
context 'success' do
let(:my_param) { [{"job1" => "run"}, {"job2" => "run again"}] }
it 'should pass' do
test_result = job.run_job(my_param)
expect(test_result[0]["job1"]).to eq("run")
end
end
end
end
块前使用
describe Job do
let(:job) { Job.new }
describe 'run_job' do
context 'success' do
before(:all) do
@my_param = [{"job1" => "run"}, {"job2" => "run again"}]
end
it 'should pass' do
test_result = job.run_job(@my_param)
expect(test_result[0]["job1"]).to eq("run")
end
end
end
end
Better Specs推荐使用let
赋值变量:
describe Job do
let(:job) { Job.new }
describe 'run_job' do
context 'success' do
let(:my_param) { [{"job1" => "run"}, {"job2" => "run again"}] }
it 'should pass' do
test_result = job.run_job(my_param)
expect(test_result[0]["job1"]).to eq("run")
end
end
end
end