我正在尝试整个BDD
方法,并想测试我正在编写AMQP
的香草应用程序的 -based 方面。Ruby
在选择Minitest
作为测试框架以平衡其功能和表现力而不是其他恰当命名的蔬菜框架之后,我开始编写这个规范:
# File ./test/specs/services/my_service_spec.rb
# Requirements for test running and configuration
require "minitest/autorun"
require "./test/specs/spec_helper"
# External requires
# Minitest Specs for EventMachine
require "em/minitest/spec"
# Internal requirements
require "./services/distribution/my_service"
# Spec start
describe "MyService", "A Gateway to an AMQP Server" do
# Connectivity
it "cannot connect to an unreachable AMQP Server" do
# This line breaks execution, commented out
# include EM::MiniTest::Spec
# ...
# (abridged) Alter the configuration by specifying
# an invalid host such as "l0c@alho$t" or such
# ...
# Try to connect and expect to fail with an Exception
MyApp::MyService.connect.must_raise EventMachine::ConnectionError
end
end
我已经注释掉了包含em-minitest-spec gem的功能,它应该强制规范在EventMachine
反应器内运行,如果我包含它,我会遇到一个关于(我想)内联类等的更粗略的异常:NoMethodError: undefined method 'include' for #<#<Class:0x3a1d480>:0x3b29e00>
.
我正在测试的代码,即该connect
服务中的方法基于这篇文章,如下所示:
# Main namespace
module MyApp
# Gateway to an AMQP Server
class MyService
# External requires
require "eventmachine"
require "amqp"
# Main entry method, connects to the AMQP Server
def self.connect
# Add debugging, spawn a thread
Thread.abort_on_exception = true
begin
@em_thread = Thread.new {
begin
EM.run do
@connection = AMQP.connect(@settings["amqp-server"])
AMQP.channel = AMQP::Channel.new(@connection)
end
rescue
raise
end
}
# Fire up the thread
@em_thread.join
rescue Exception
raise
end
end # method connect
end
end # class MyService
整个“异常处理”只是试图将异常冒泡到我可以捕获/处理它的地方,这也无济于事,无论有没有begin
andraise
位,在运行规范时我仍然得到相同的结果:
EventMachine::ConnectionError: unable to resolve server address
,这实际上是我所期望的,但Minitest
与整个反应堆概念并没有很好地配合,并且因此未能通过测试Exception
。
那么问题仍然存在:一个测试EventMachine
相关的代码如何使用Minitest
的规范机制?另一个问题也一直在徘徊Cucumber
,也没有答案。
或者我应该专注于我的主要功能(例如消息传递和查看消息是否被发送/接收)而忘记边缘情况?任何见解都会真正有帮助!
当然,这一切都可以归结为我上面写的代码,也许这不是编写/测试这些方面的方式。可能!
关于我的环境的注意事项:(是的,ruby 1.9.3p194 (2012-04-20) [i386-mingw32]
Win32 :>)minitest 3.2.0
,,,,eventmachine (1.0.0.rc.4 x86-mingw32)
amqp (0.9.7)
提前致谢!