[英]stubbing a class method inside a method in rspec to return true
here is the class for which i'm writing a rspecs.这是我正在为其编写 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
i want to stub a class method to return true.我想存根 class 方法以返回 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
here is the error im getting这是我得到的错误
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
I just want to mock my class method to return true.我只想模拟我的 class 方法以返回 true。 I dont want to test the class methods of SiteLicenseService .我不想测试 SiteLicenseService 的class方法。 I dont want to create site license with package literally.我不想从字面上用 package 创建站点许可证。
can anyone explain me what is the mistake i'm doing.谁能解释我在做什么错误。
Thanks谢谢
Ensure the syntaxing of mocking the return value is correct:确保 mocking 返回值的语法正确:
allow(SiteLicenseService).to
receive(:get_package_for_site_license).and_return(true)
In Ruby, classes are written in TitleCase and methods are written in snake_case.在 Ruby 中,类是用 TitleCase 编写的,方法是用 snake_case 编写的。 The method to be received should be a:symbol.要接收的方法应该是一个:符号。
I chnaged the stubbing like this and it started working我像这样改变了存根,它开始工作
before do
allow(SiteLicenseService).to receive(:get_package_for_site_license).and_return(true)
end
now it does not look for a relation.现在它不寻找关系。 but i appreciate if anyone tells me what changed internally.但如果有人告诉我内部发生了什么变化,我将不胜感激。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.