I am writing an ATM-system-like socket/server solution. I would appreciate if someone could tell me what I'm missing. For some reason, I get the following error running my stub test suite:
我正在编写类似ATM系统的套接字/服务器解决方案。如果有人能告诉我我错过了什么,我将不胜感激。出于某种原因,运行我的存根测试套件时出现以下错误:
# Running tests:
.E
Finished tests in 0.002411s, 829.4384 tests/s, 414.7192 assertions/s.
1) Error:
test_0001_connects_to_a_host_with_a_socket(AtmClient::connection):
NoMethodError: undefined method `new' for #<SpoofServer:0x9dce2dc @clients=[], @server=#<TCPServer:fd 5>>
/media/wildfyre/Files/Programming/KTH/progp/Atm/spec/client/SpoofServer.rb:12:in `start'
/media/wildfyre/Files/Programming/KTH/progp/Atm/spec/client/client_spec.rb:12:in `block (3 levels) in <top (required)>'
2 tests, 1 assertions, 0 failures, 1 errors, 0 skips
My minispec file is:
我的minispec文件是:
require_relative '../spec_helper.rb'
require_relative '../../lib/AtmClient.rb'
require_relative 'SpoofServer.rb'
describe AtmClient do
it "can be created with no arguments" do
AtmClient.new.must_be_instance_of AtmClient
end
describe 'connection' do
it "connects to a host with a socket" do
spoof = SpoofServer.new.start
client = AtmClient.new.connect
spoof.any_incoming_connection?.must_be true
spoof.kill
end
end
end
My SpoofServer file is:
我的SpoofServer文件是:
require 'socket'
class SpoofServer
def initialize
end
def start
@clients = []
@server = TCPServer.new 1234
@listener_thread = new Thread do
@clients.add @server.accept
end
end
def any_incoming_connection?
@clients.size > 0
end
def kill
@listener_thread.exit
@clients.each {|c| c.close}
end
end
2 个解决方案
#1
12
As you can read in the trace of the calls stack:
正如您可以在调用堆栈的跟踪中阅读:
NoMethodError: undefined method `new' for #<SpoofServer:...>
/.../spec/client/SpoofServer.rb:12:in `start'
The error is inside the start
method defined in SpoofServer.rb
, at line 12, the wrong line is:
错误在SpoofServer.rb中定义的start方法内,在第12行,错误的行是:
@listener_thread = new Thread do
That should be:
那应该是:
@listener_thread = Thread.new do
As you have written it, what you are actually doing is to calling the new
method passing the Thread
class as argument. Since no new
method is defined for instances of the SpoofServer
class you get the NoMethodError
exception.
正如您所编写的那样,您实际执行的操作是调用将Thread类作为参数传递的新方法。由于没有为SpoofServer类的实例定义新方法,因此会出现NoMethodError异常。
#2
0
In body of instance method SpoofServer#start
, you can't call the class method SpoofServer.new
by new
.
在实例方法SpoofServer #start的主体中,你不能用new调用类方法SpoofServer.new。
#1
12
As you can read in the trace of the calls stack:
正如您可以在调用堆栈的跟踪中阅读:
NoMethodError: undefined method `new' for #<SpoofServer:...>
/.../spec/client/SpoofServer.rb:12:in `start'
The error is inside the start
method defined in SpoofServer.rb
, at line 12, the wrong line is:
错误在SpoofServer.rb中定义的start方法内,在第12行,错误的行是:
@listener_thread = new Thread do
That should be:
那应该是:
@listener_thread = Thread.new do
As you have written it, what you are actually doing is to calling the new
method passing the Thread
class as argument. Since no new
method is defined for instances of the SpoofServer
class you get the NoMethodError
exception.
正如您所编写的那样,您实际执行的操作是调用将Thread类作为参数传递的新方法。由于没有为SpoofServer类的实例定义新方法,因此会出现NoMethodError异常。
#2
0
In body of instance method SpoofServer#start
, you can't call the class method SpoofServer.new
by new
.
在实例方法SpoofServer #start的主体中,你不能用new调用类方法SpoofServer.new。