首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用rspec成功模拟Celluloid

如何用rspec成功模拟Celluloid
EN

Stack Overflow用户
提问于 2018-03-28 22:57:42
回答 1查看 163关注 0票数 2

好吧,我累坏了。在某种意义上说,已经筋疲力尽了。

我们有管理参与者的Supervisor。

代码语言:javascript
复制
supervisor = Celluloid::SupervisionGroup.run!

airbrake = supervisor.pool(Flango::AirbrakeActor, as: :airbrake_actor, size: 1)

我所需要做的就是模拟具有notify_exception方法的空气制动actor。在其中定义了。

即下面的调用

代码语言:javascript
复制
airbrake.async.notify_exception('exception') 

相关的rspec代码...

代码语言:javascript
复制
expect(airbrake.async).to receive(:notify_exception).with('exception')

我已经尝试过this..不工作

尝试了以下方法(不确定我在做什么)

代码语言:javascript
复制
airbrake = OpenStruct.new(:async, Flango::AirbrakeActor.new)

这项工作,但测试挂在最后,直到被杀死。

有什么帮助吗?

EN

回答 1

Stack Overflow用户

发布于 2018-04-07 19:59:29

经过两次更改后,我能够让它正常工作。

其中一个问题是,你关闭了赛萝卜线,然后使用了死变量。所以我在init中重构了启动代码。

代码语言:javascript
复制
require 'bundler'
Bundler.setup(:default)
require 'ffi-rzmq'
require 'celluloid/zmq'
require 'celluloid/current'
require 'securerandom'
Celluloid::ZMQ.init
class SockOne
  include Celluloid::ZMQ
  attr_reader :sock
  def initialize
    @sock = PullSocket.new
    @sock.connect('tcp://127.0.0.1:20483')
  end

  def read
    loop do
      middleware.async.start(sock.read_multipart)
    end
  end
end

class SockTwo
  include Celluloid::ZMQ
  attr_reader :sock
  def initialize
    @sock = PushSocket.new
    @sock.connect('tcp://127.0.0.1:20484')
  end

  def send_response(other_id, id, response)
    start_time = Time.now
    sock.send(response)
  end
end

class Middleware
  include Celluloid
  def start(data)
    puts "i am starting middleware with " + data
    Handler.new.start(data)
  end
end

class Handler
  def start(data)
    begin
      execute(data)
    rescue => exception
      airbrake.async.notify('some exception')
    ensure
      sock_two.async.send_response(['i got a reply'])
    end
  end

  def execute(data)
    data
  end
end

class Airbrake
  include Celluloid
  def notify(data)
    puts "this is notify to airbrake - " + data
  end
end

def init
  supervisor = Celluloid::SupervisionGroup.run!
  $middleware = supervisor.pool(Middleware, as: :middleware, size: 10)
  $sock_two = supervisor.pool(SockTwo, as: :sock_two, size: 10)
  $airbrake = supervisor.pool(Airbrake, as: :airbrake, size: 1)
end

def airbrake
  $airbrake
end

def sock_two
  $sock_two
end

def middleware
  $middleware
end

if $0 == __FILE__
  puts "Starting.."
  sock_one = SockOne.new
  sock_one.read
end

然后重构main_spec.rb,如下所示

代码语言:javascript
复制
require 'rspec'

require_relative 'main'

describe 'Handler' do
  describe '#start' do
    before(:each) do
      Celluloid.shutdown
      #init()
      Celluloid.boot
      init()
    end

    it 'should invoke async call to celluloid' do
      handler = Handler.new
      expect(handler).to receive(:execute).with(['1', '2', '3']).and_raise('boom')

      # This does not work
       airbrake.wrapped_object.instance_eval do
         def notify(data)
            puts "new data was called - " + data
         end
       end

      expect(airbrake.wrapped_object).to receive(:notify).with('some exception')

      handler.start(['1', '2', '3'])
    end

    after(:each) do
      Celluloid.shutdown
    end
  end
end

现在它起作用了

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49537879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档