NetworkDaemon
的类,它是NetworkDaemonSpecification
中的相关规范。很简单。
但是,我的问题是我似乎无法使用require 'tcp_server'
中的network_daemon.rb
,但我可以network_daemon_specification.rb
。
如果我尝试引用tcp_server
中的network_daemon.rb
(在network_daemon_specification.rb
中进行了单元测试,则会得到以下stacktrace
E:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require': cannot load such file -- tcp_server (LoadError)
from E:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require'
from E:/Ruby/RubyCraft/network/network_daemon.rb:1:in `<top (required)>'
from E:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require'
from E:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require'
from E:/Ruby/RubyCraft/specification/network_daemon_specification.rb:1:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
但是,我可以在tcp_server
中引用network_daemon_specification
就好了。这是怎么回事? d:
network_daemon.rb
require 'tcp_server'
module RubyCraft
module Network
# The network daemon controls the listening and responses
class NetworkDaemon
attr_reader :port
# Creates the NetworkDaemon from the given port
# @param [Fixnum] port
def initialize port
@port = port
@sock = TCPServer.new @port
end
# Closes the NetworkDaemon
def close
@sock.close
end
end
end
end
network_daemon_specification.rb
# Unit tests for the Network Daemon that will be used to listen for connections
module RubyCraft
module Specifications
class NetworkDaemonSpecification < Test::Unit::TestCase
def setup
@port = 9999
@daemon = NetworkDaemon.new @port
end
def test_port_should_equal_injected_port
assert_equal @daemon.port, @port
end
def teardown
@daemon.close
end
end
end
end
答案 0 :(得分:0)
结果tcp_server
不是你需要的(尽管RubyMine说的是) - 你应该要求socket
答案 1 :(得分:0)
您需要require 'socket'
而不是tcp_server
查看TCPServer
文档以获取更多信息http://www.ruby-doc.org/stdlib-1.9.3/libdoc/socket/rdoc/TCPServer.html