我需要為具有私有方法并采用單個引數的模塊撰寫 RSpec 測驗用例。
module x
private
def fun(para)
end
end
我有一個規范檔案,我試圖在其中撰寫這樣的案例。para 和 params 是我們可以說的論點。
RSpec.describe x do
class spec
include x
def initialize(params)
@params = params
end
end
describe "describe" do
context "context" do
it "should not truncate any normal text value" do
obj = spec.new(params)
# first
expect('dummy text').to obj.send(:fun).with(para)
# second
expect(obj).to receive(:fun).with(para);
#third
retr = obj.send(:fun).with(para);
expect retr.to eq('dummy text')
end
end
end
end
第一,第二和第三,我曾經得到輸出,但這三種方式都不起作用。他們都在拋出一些錯誤。伙計們,你能幫我理解我做錯了什么嗎?我該如何解決這個問題?
uj5u.com熱心網友回復:
如果一個方法是private你不應該直接測驗它,但在某些情況下,為什么不呢。??我不知道你的fun方法應該做什么,但這里有一些幫助。
RSpec.describe x do
class MySpec
include x
def initialize(params)
@params = params
end
end
describe "#fun" do
subject { my_spec.send(:fun).with(para) }
let(:my_spec) { MySpec.new(params) }
let(:params) { { foo: :bar } }
let(:para) { 'dummy text' }
it "should not truncate any normal text value" do
expect(subject).to eq(para)
end
end
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/511825.html
