Why am I getting "Undefined method :: new" in simple classes?

I am writing a socket / server solution similar to an ATM system. I would be grateful if someone would tell me what I was missing. For some reason, I get the following error when starting my test suite:

# 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:

 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:

 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 
+4
source share
2 answers

As you can read in the call stack trace:

 NoMethodError: undefined method `new' for #<SpoofServer:...> /.../spec/client/SpoofServer.rb:12:in `start' 

The error is inside the start method defined in SpoofServer.rb , line 12 has an invalid line:

 @listener_thread = new Thread do 

It should be:

 @listener_thread = Thread.new do 

As you wrote it, what you are actually doing is calling the new method, passing the Thread class as an argument. Since the new method is not defined for instances of the SpoofServer class, you get a NoMethodError exception.

+12
source

In the body of an instance of the SpoofServer#start method, you cannot call the method of the SpoofServer.new class on new .

0
source

Source: https://habr.com/ru/post/1480700/


All Articles