← Back to Upcase

How do you test a method that returns nil or nothing?

(Charlieanna) #1

I have this method which does the work of sending a push notification.

 def push
      host = 'gateway.push.apple.com'
      path = @cert_name
      ssl_client = Pushofy::ConnectionToAppleServer::ssl_connect(@host,@port,              @password,'../DevCert.pem')
      device = [@deviceTokenHex]
      device_token_binary = device.pack('H*')
      pt = device_token_binary
      pm = @payload_hash.to_json
      message = [0, 0, 32, pt, 0, pm.size, pm].pack('ccca*cca*')

How can I test that this does what it is expected to do? At the end it is simply sending sending a message to the apple server.

This method is defined on a class in the gem.

(Robert Lee-Cann) #2

I don’t think you can test a method that returns nothing, as a passing test would reveal nothing useful.

As this method only calls the Pushofy module, the tests are probably better to go in there. It also feels to me that all of this code is related to pushing the notification so is best placed inside of Pushofy.

Also, you might like to consider how you test when the Apple push notification service fails, as remote API calls are never 100% reliable. You could then test how your ‘push’ method copes with a successful and failing call to Pushofy?

(Joe Ferris) #3

You can use test doubles to unit test methods that have side effects but no return value.

You can stub out the instantiation of Pushofy::ConnectionToAppleServer.ssl_connect and then verify that it receives the correct method invocations like write and flush.