将 rspec 中的方法内的 class 方法存根为 return true
stubbing a class method inside a method in rspec to return true
这是我正在为其编写 rspecs 的 class。
# frozen_string_literal: true. Thiis is the method **SiteLicenseService.get_package_for_site_license site_license_id, package_id**
module SiteLicenseGrants
# Raise an error if no site license is found for a given package
class Package
# Fail if the given packages are not found or if they are not associated
# with the site license
def self.valid_package?(opts)
package_id = opts[:package_id]
site_license_id = opts[:site_license_id]
return true if SiteLicenseService.get_package_for_site_license site_license_id, package_id
rescue Errors::EntityNotFound
raise Errors::NoSiteLicenseForPackage.new package_id # rubocop:disable Style/RaiseArgs
end
end
end
我想将 class 方法存根为 return true。
require "rails_helper"
RSpec.describe SiteLicenseGrants::Package do
describe "check valid package" do
context "valid package" do
let(:package_id) { 1 }
let(:site_license_id) { 1 }
let(:opts) { { package_id: package_id, site_license_id: site_license_id } }
before do
site_license_service = class_double("SiteLicenseService")
allow(site_license_service).to receive(:get_package_for_site_license).with(site_license_id, package_id).and_return(true)
end
it "returns true" do
expect do
described_class.valid_package?(opts)
end.to be_truthy
end
end
context "invalid package" do
let(:opts) { { package_id: nil, site_license_id: nil } }
it "throws error" do
expect do
described_class.valid_package?(opts)
end.to raise_error(Errors::NoSiteLicenseForPackage)
end
end
end
end
这是我得到的错误
Failures:
1) SiteLicenseGrants::Package check valid package valid package returns true
Failure/Error: raise Errors::NoSiteLicenseForPackage.new package_id # rubocop:disable Style/RaiseArgs
Errors::NoSiteLicenseForPackage:
Package 1 doesn't have a site license associated with it
我只想将我的 class 方法模拟为 return true。我不想测试 SiteLicenseService 的 class 方法。我不想从字面上创建带有包的站点许可证。
谁能解释一下我做错了什么。
谢谢
确保模拟 return 值的语法正确:
allow(SiteLicenseService).to
receive(:get_package_for_site_license).and_return(true)
在Ruby中,类是TitleCase,方法是snake_case。要接收的方法应该是 :symbol.
我像这样更改了存根,它开始工作了
before do
allow(SiteLicenseService).to receive(:get_package_for_site_license).and_return(true)
end
现在它不寻找关系。但如果有人告诉我内部发生了什么变化,我将不胜感激。
这是我正在为其编写 rspecs 的 class。
# frozen_string_literal: true. Thiis is the method **SiteLicenseService.get_package_for_site_license site_license_id, package_id**
module SiteLicenseGrants
# Raise an error if no site license is found for a given package
class Package
# Fail if the given packages are not found or if they are not associated
# with the site license
def self.valid_package?(opts)
package_id = opts[:package_id]
site_license_id = opts[:site_license_id]
return true if SiteLicenseService.get_package_for_site_license site_license_id, package_id
rescue Errors::EntityNotFound
raise Errors::NoSiteLicenseForPackage.new package_id # rubocop:disable Style/RaiseArgs
end
end
end
我想将 class 方法存根为 return true。
require "rails_helper"
RSpec.describe SiteLicenseGrants::Package do
describe "check valid package" do
context "valid package" do
let(:package_id) { 1 }
let(:site_license_id) { 1 }
let(:opts) { { package_id: package_id, site_license_id: site_license_id } }
before do
site_license_service = class_double("SiteLicenseService")
allow(site_license_service).to receive(:get_package_for_site_license).with(site_license_id, package_id).and_return(true)
end
it "returns true" do
expect do
described_class.valid_package?(opts)
end.to be_truthy
end
end
context "invalid package" do
let(:opts) { { package_id: nil, site_license_id: nil } }
it "throws error" do
expect do
described_class.valid_package?(opts)
end.to raise_error(Errors::NoSiteLicenseForPackage)
end
end
end
end
这是我得到的错误
Failures:
1) SiteLicenseGrants::Package check valid package valid package returns true
Failure/Error: raise Errors::NoSiteLicenseForPackage.new package_id # rubocop:disable Style/RaiseArgs
Errors::NoSiteLicenseForPackage:
Package 1 doesn't have a site license associated with it
我只想将我的 class 方法模拟为 return true。我不想测试 SiteLicenseService 的 class 方法。我不想从字面上创建带有包的站点许可证。
谁能解释一下我做错了什么。
谢谢
确保模拟 return 值的语法正确:
allow(SiteLicenseService).to
receive(:get_package_for_site_license).and_return(true)
在Ruby中,类是TitleCase,方法是snake_case。要接收的方法应该是 :symbol.
我像这样更改了存根,它开始工作了
before do
allow(SiteLicenseService).to receive(:get_package_for_site_license).and_return(true)
end
现在它不寻找关系。但如果有人告诉我内部发生了什么变化,我将不胜感激。