Django-channels, how to use mocks - django-channels

I build this chat website, and I'd like to test this functionnality:
if you send a message and the user is online, send message via websocket: Tested
if you send a message and the user is offline, send a push notification (it's a REST call).
Obviously, in my test, I don't want to do the REST call. I'd like to mock the function "push_notif".
But, when I use unittest.patch, the function is not mocked in the consumer (probably because of some async stuff). How can I mock this "push_notif" function

Out of solution, I made a decorator for my function "push_notif". If in test, it writes in a file "func.__name__,args,kwargs", then I read this file in my test to see if the right call was passed.
Ugly, but getting things done

Related

Cannot get a task to dequeue a call and connect to javascript client device

I've so far been able to capture an incoming call, and add it to a queue with a workflow reference.
After that the Javascript client I have connected with a worker and a device can see the reservation being created, at which point I call reservation.accept()
I can see that Twilio is calling my assignment callback url, where I am returning this from express
res.status(200).json({
instruction: "dequeue",
to: res.locals.twilioIdentity,
})
the twilioIdentity here is the same one I've attached to the accessToken that gets generated and used to create both the device and worker on the Javascript app.
I see in the tasks view in the console that the status has moved to accepted and my worker is the one that now has it, but the call remains on hold and nothing happens on the Javascript app
After I get a ready event when creating the worker, I call
readyWorker.setAttributes({
contact_uri: identity
})
identity here is the same as above that got used to generate the token and is being passed with the dequeue instruction
What am I missing? The docs don't seem to point to a comprehensive example on dequeueing a call and connecting it to a web based Javascript client
I was misunderstanding dequeue and conference. I was able to make this work by using the conference instruction instead and making sure any reference to a client identity I was passing was prefixed with client: for example contact_uri: client:${identity}

Avoiding multiple API Calls in Rspec tests

I am trying to test a "scan" method that I have. Within this scan method, I make an API call (among other things).
How can I test this method without triggering unneeded API calls?
One approach is to just stub out the call to the API:
allow(thing).to receive(:action).and_return(response)
Another approach is to allow the API call to go through, but to intercept it and return a mock response using VCR. To do this you "record" a request and "play it back".
VCR is handy when you need to handle the the entire response in the test subject. Just run the test against the real API one time, then subsequent tests can use the VCR "cassette". OTOH this is slower than simply stubbing the call, especially if you only need to mock the status and not the entire response.
TL:DR, stub if you can, but don't hesitate to use VCR when it saves you work.
You shouldn't be making API calls in your test environment. In order to prevent these calls, you should stub the method so that it return a true or success upon being called.

Test a method which uses external API

I have a model called Video. This model has a after_save callback which runs a method #upload_video_to_depot. That method uses :file param (which is not saved in the db) and uploads a video file to a remote API using RestClient.
The question is - how to handle that in my specs without actually sending the file to the API ? I need to test my Video model and #upload_video_to_depot method but i can't imagine how it should be done (i'm quite fresh in the TDD thing).
Can it be fully handled in the specs or it also involves some changes in my model?
Use a gem like WebMock to stub external requests. You can set expectations on what requests were sent and with what params, as well as the responses the server should be giving you.
This will allow you to ensure that your REST client is sending the correct params to the correct place and processing responses correctly.

How can you use OCMock with NSURLConnection/delegate for a series of mock network connections?

I've implemented the code in this posting:how to unit test a NSURLConnection Delegate?
and have managed to get a test case going with my code where I simulate sending a server package of data via the mock classes.
This is ok to test a simple single client post / server response type situation but I want to test a conversation scenario where my code makes a post, the server sends a reply, my code makes another response, the server sends another reply etc.
Has anybody done anything similar to this?

iOS: how to do indeterminate asynchronous unit test with GHUnit?

I use GHUnit for unit testing, and wrote subclasses of GHAsyncTestCase, but in my case, the web service is not like an active service request where you send request and expect to receive an answer, instead of this, my web service is a passive type service where you subscribe to a channel then it can send you message, but there is no guarantee about how frequent the server pushes messages to subscribers, in this case, say 50 secs after subscribing, server pushes a message and another message after 30 secs, then should I do:
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:50.0f];
to verify the first message, or is there any smart way for doing this?
Thanks!
It depends on what you want to do.
If you want to verify the data coming from your server, you can send a request to the server to ask it push something immediately and use
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:50.0f];
to wait for the pushed messages.
It you want to verify your objective-c code, you can send some fake data to your object.
If you are using NSURLConnection to get your data, call the delegate method below with some fake data in your testing code
- (void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)data
The idea behind unit testing is "unit" testing. The testing code should focus on the "unit" you want to test. It is a good practice to exclude other uncontrollable factors (like network availability) from your testing.

Resources