我在两个单独的模块中有两个类(我知道现在这不是一个很好的用途:/)我有这样的东西:
module MQ
class Client
def self.start(opts = {})
new(opts).start
end
def initialize(queue, message)
@template_message = message
@queue = queue
end
def start
EventMachine.run do
#some code to send message via AMQP
Signal.trap("INT") { connection.close { EventMachine.stop { exit } }}
Signal.trap("TERM") { connection.close {EventMachine.stop { exit(0) } }}
Signal.trap("INFO") { puts "Current active statements: #{statements.keys.inspect}" }
end
end
def stop
EventMachine.stop
end
end
end
接下来我定义了服务器类:
module Esper
class Server
def self.start(opts = {})
new(opts).start
end
def initialize(options)
end
def start
EventMachine.run do
#some code here to receive messages
Signal.trap("INT") { connection.close { EventMachine.stop { exit } }}
Signal.trap("TERM") { connection.close {EventMachine.stop { exit(0) } }}
Signal.trap("INFO") { puts "Current active statements: #{statements.keys.inspect}" }
end
end
def stop
EventMachine.stop
end
end
end
现在我有 rspec (这里是错误报告):
context "matched messages" do
before :each do
@template_message = { }
@server = Esper::Server.new
@client = MQ::Client.new("queue_name", @template_message)
end
describe "transfer" do
it "should receive statements" do
Thread.new do
@server.start
end
Thread.new do
@client.start
end
puts "Sleep for 6 seconds"
sleep(6.0)
#some check here
@server.stop
@client.stop # and here it reports when I am trying to access nil class in class client in method stop.
end
end
它在尝试调用时Client
在方法中的类中报告
它说:stop
EventMahine.stop
undefined method `stop' for nil:NilClass
有人可以指出我错在哪里,如果您有任何建议如何解决它?