为具有 if/else 逻辑的方法添加 RSpec 覆盖率
Adding RSpec coverage for a method with if/else logic
所以我对 RSpec 和 Rails 还很陌生,我一直在尝试尽可能多地学习 RSpec,我真的很难实现覆盖方法其中包含逻辑。
我正在练习的应用程序使用覆盖百分比来确保我正确地覆盖了我正在实施的代码并且我缺少对以下方法的覆盖:
def initialize_business
businesses.each do |business|
if business.type == 'Restaurant'
@business_type = Business::Restaurant.new(json_directory: 'restaurant.json')
elsif business.type = 'Bar'
@business_type = Business::Bar.new(json_directory: 'bar.json')
else
@business_type = Business::Other.new(json_directory: 'other_business.json')
end
end
business_type = @business_type
initialize_business_creator(business_type)
end
我最初尝试提供覆盖范围(忽略了其他不相关的规范),但我什至难以实现任何覆盖范围,因为我对 RSpec 太陌生了:
describe '#initialize_business' do
subject do
described_class.new([business], business_sample_file).
initialize_business_creator
end
it 'assigns a value to @business_type' do
expect(assigns(@business_type)).to_not be_nil
end
end
end
我只是在寻求一些帮助 and/or 有关如何为此类方法实施规范的指导,我感谢所有帮助。谢谢!
You need to create scenarios to test the branches of your code (if
,
elsif
, else
)
你可以做的是,你可以 mock
方法 returning type
以获得你想要的结果。
例如,如果您想测试您的 if
条件是否已被评估并且该分支中的代码 运行 成功。
你可以这样做:
describe '#initialize_business' do
subject do
described_class.new([business], business_sample_file).
initialize_business_creator
end
it 'assigns a value to @business_type' do
expect(assigns(@business_type)).to_not be_nil
end
context "when business type is 'Restaurant'" do
before { allow_any_instance_of(Business).to receive(:type).and_return "Restaurant"
end
it "should create data from restaurant.json" do
//here you can write expectations for your code inside if statement
end
end
end
行:
allow_any_instance_of(Business).to receive(:type).and_return "Restaurant"
每当调用 business.type
时, 将 return 一个 "Restaurant" 字符串。
以同样的方式,您可以将此方法设为 return 其他值,例如 "Bar" 并检查您的 elsif
场景。
希望对您有所帮助。
所以我对 RSpec 和 Rails 还很陌生,我一直在尝试尽可能多地学习 RSpec,我真的很难实现覆盖方法其中包含逻辑。
我正在练习的应用程序使用覆盖百分比来确保我正确地覆盖了我正在实施的代码并且我缺少对以下方法的覆盖:
def initialize_business
businesses.each do |business|
if business.type == 'Restaurant'
@business_type = Business::Restaurant.new(json_directory: 'restaurant.json')
elsif business.type = 'Bar'
@business_type = Business::Bar.new(json_directory: 'bar.json')
else
@business_type = Business::Other.new(json_directory: 'other_business.json')
end
end
business_type = @business_type
initialize_business_creator(business_type)
end
我最初尝试提供覆盖范围(忽略了其他不相关的规范),但我什至难以实现任何覆盖范围,因为我对 RSpec 太陌生了:
describe '#initialize_business' do
subject do
described_class.new([business], business_sample_file).
initialize_business_creator
end
it 'assigns a value to @business_type' do
expect(assigns(@business_type)).to_not be_nil
end
end
end
我只是在寻求一些帮助 and/or 有关如何为此类方法实施规范的指导,我感谢所有帮助。谢谢!
You need to create scenarios to test the branches of your code (
if
,elsif
,else
)
你可以做的是,你可以 mock
方法 returning type
以获得你想要的结果。
例如,如果您想测试您的 if
条件是否已被评估并且该分支中的代码 运行 成功。
你可以这样做:
describe '#initialize_business' do
subject do
described_class.new([business], business_sample_file).
initialize_business_creator
end
it 'assigns a value to @business_type' do
expect(assigns(@business_type)).to_not be_nil
end
context "when business type is 'Restaurant'" do
before { allow_any_instance_of(Business).to receive(:type).and_return "Restaurant"
end
it "should create data from restaurant.json" do
//here you can write expectations for your code inside if statement
end
end
end
行:
每当调用allow_any_instance_of(Business).to receive(:type).and_return "Restaurant"
business.type
时,将 return 一个 "Restaurant" 字符串。
以同样的方式,您可以将此方法设为 return 其他值,例如 "Bar" 并检查您的 elsif
场景。
希望对您有所帮助。