Я пытаюсь запустить некоторые тесты на свой код, но у меня проблема. Для очень простой строки кода я получаю странное сообщение об ошибке. Этот тест должен удостовериться, что мой сервер может получать информацию от одного из моих клиентов.Как исправить это сообщение об ошибке? синтаксическая ошибка, неожиданный tIVAR
испытания и запустить файл нормально без этой линии:
[email protected]("This gives an error.")
В том числе, что бит кода дает эту ошибку:
macowner:WAR3 macowner$ ruby ServerTests1.rb
ServerTests1.rb:160: syntax error, unexpected tIVAR, expecting '('
[email protected]("Output for Server to receive.") #Error
^
Помощь очень ценится. Я получаю эту ошибку довольно часто, но я понятия не имею, что это значит (даже при ее поиске).
enter code here
require 'minitest/autorun'
require 'socket'
require_relative 'WarGame_Class.rb'
require_relative 'ModifiedPlayer_Class.rb'
require_relative 'DeckClass.rb'
class WarServer
def initialize(host, port)
@socket_server = TCPServer.new(host, port)
@players = [Player.new, Player.new]
@deck = CardDeck.new
@deck.deal_cards(@players[0].cards, @players[1].cards)
game = WarGame.new
@clients = {} # keys are sockets, values are players
end
def client_keys(key)
@clients.keys[key] # this should work
end
def input #input reader function
@input
end
def close
@socket_server.close
end
def capture_input ##input client to get what they wrote
@input = @clients.keys[0].read_nonblock(1000) # arbitrary max number of bytes
end
def accept_client
#Hash here to link client to player? (or game?)
client = @socket_server.accept
@clients[client] = @players[@clients.size]
# puts "clients key 0: #{@clients.keys[0]}"
puts
# puts "clients values: #{@clients.values}"
if @clients.size == 2
start_game#####################!!!! Starts game if two clients can put client messages in start game
end
end
def start_game ##############!!!
@clients.keys[0].puts "Welcome to War. Please press enter to play your card"
@clients.keys[1].puts "Welcome to War. Please press enter to play your card"
end
end
class MockWarClient
def initialize
@socket = TCPSocket.new('localhost', 2012)
end
def output
@output
end
def input
@input
end
def capture_output #need to add (socket)? How else read from specific socket?
@output = @socket.read_nonblock(1000) # arbitrary max number of bytes
rescue
@output = "capture_output error."
end
def write_input
@input = @war_server.client_keys.write_nonblock(1000)
end
end
class WarServerTest < MiniTest::Unit::TestCase
def setup #This would be like our INITIALIZE Function
#anything is available through out all tests (i.e., instance vars)
@war_server = WarServer.new('localhost', 2012)
end
def teardown
@war_server.close
end
def test_server_capture_output_from_client
client_1 = MockWarClient.new
@war_server.accept_client
client_2 = MockWarClient.new
@war_server.accept_client
[email protected]("Output for Server to receive.") #Line I need to fix
#SOCKETforSERVER
#clien_n.SOCKETforSERVER.puts''
#Replace puts with write_nonblock, perhaps
end
end
Я не думаю, что они пытаются вызвать метод с именем '@ socket', они явно пытаются получить доступ к переменной экземпляра. –
Это должен быть метод, потому что вы никогда не отправляете сообщение, используя точку с переменной любого типа. Для анализатора это должен быть метод. – vgoff
@mu слишком коротко Я довольно любительский программист ... aaaand Я думаю, что я пытаюсь вызвать переменную там (потому что я пытаюсь передать информацию через сокет ...) – Yallo