【问题标题】:Testing a job in Dashing在 Dashing 中测试工作
【发布时间】:2018-06-09 03:27:16
【问题描述】:

我现在正在使用 Dashing/Smashing 构建一个应用程序,并且我正在使用 rspec 来测试我的代码。但是,我无法弄清楚如何检查 send_event 是否被调用。我试过了

expect(Sinatra::Application).to receive(:send_event).twice

expect(Dashing).to receive(:send_event).twice,

但都没有工作。我不确定哪个对象应该接收到send_event 的调用,因为它位于app.rb 的Dashing 内部。还有this issue,在讲同样的事情,在Dashing GitHub上没有回答。

任何关于如何做到这一点的建议将不胜感激。谢谢!

更新:

我还没有弄清楚如何做到这一点,但我发现这是可行的:

let(:dummy_class) { Class.new { include Dashing } }

context 'something' do
    it 'does something' do
        expect(dummy_class).to receive(:send_event).once
        dummy_class.send('send_event', 'test', current: 'test')
    end
end

但是,如果我使用我想要调用的包含send_event 的方法而不是使用dummy_class.send(...),那么它不会识别出该方法已被调用。这一定与我的测试不使用虚拟类有关。我不知道有没有办法解决这个问题并让它使用虚拟类。

【问题讨论】:

    标签: ruby testing rspec jobs dashing


    【解决方案1】:

    我想通了!

    不要在工作中直接调用send_event。在其他类中调用它,可能称为EventSender。然后,要测试是否调用了send_event,请将其视为该类的实例方法而不是模块的方法。您的代码可能如下所示,例如:

    describe 'something' do
        context 'something' do
            it 'does something' do
                happy_es = EventSender.new(...)
                expect(happy_es).to receive(:send_event).with(...)
                happy_es.method_that_calls_sendevent
            end
        end
    end
    

    希望这可以帮助那些在同一件事上苦苦挣扎的人。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多