这是最终起作用的地方:
MyBaseParser parser = new MyBaseParser(tokenStream);
//First setup the listener.
parser.addParseListener(new MyListener());
ParseTree tree = parser.main_rule();
//Then setup the visitor
MyVisitor visitor = new MyVisitor(...);
Value result = visitor.visit(tree);
这个绝对感觉不对。但是,在运行测试时,将# lib file
module SlackWrapper
class << self
def client
@client ||= ::Slack::Web::Client.new
end
end
end
describe SlackWrapper do
# test file
before :each do
$mock_client = double("slack client").tap do |mock|
allow(mock).to receive(:channels_info) { channel_info }
end
module SlackWrapper
class << self
def client
$mock_client
end
end
end
end
describe "#should_reply?" do
describe "while channel is paused" do
it "is falsey" do
SlackWrapper.pause_channel message.channel
expect(
SlackWrapper.should_reply? message
).to be_falsey
end
end
describe "while channel is not paused" do
it "is truthy" do
expect(
SlackWrapper.should_reply? message
).to be_truthy
end
end
end
end
保留为本地变量会给我$mock_client
,将undefined local variable
代码移到Monkeypatch中会得到double...
。当然,是猴子修补程序。
正确的方法是什么?
答案 0 :(得分:1)
您可以stub new
方法来测试块或整个规范文件:
# test file
# you could also create a class double if you need its methods:
# https://relishapp.com/rspec/rspec-mocks/v/3-9/docs/verifying-doubles/using-a-class-double
let(:slack_client) { double("slack client") }
before(:each) do
allow(::Slack::Web::Client).to receive(:new).and_return(slack_client)
end
# simple example:
it "checks slack client method to be a double" do
expect(SlackWrapper.client).to be(slack_client)
end