cofonseca / HipChatAdmin

A PowerShell module designed to interact with Atlassian HipChat.
6 stars 2 forks source link

Mock API call #11

Closed mdavis332 closed 7 years ago

mdavis332 commented 7 years ago

First pass at mocking Invoke-WebRequest so API calls are simulated. I've cobbled this together but haven't yet tested. If @cofonseca can test and confirm it's working, then it can be expanded to other Pester tests with Invoke-WebRequest commands.

cofonseca commented 7 years ago

Thanks for the contribution. Looks good at first glance. I'll test this out tomorrow when I have a bit more time and merge it in if everything looks good.

cofonseca commented 7 years ago

The mock appears to be working, but the test fails. Here's a screenshot of the result I saw: http://prntscr.com/gt7cj7

mdavis332 commented 7 years ago

Adjusted the PR to try to address the failure indicated in your screenshot. I prob need to find time to (learn to) setup Pester in my own local env to better test.

cofonseca commented 7 years ago

@mdavis332 no worries! Think I found the issue. I commented on the PR. Let me know what you think.

cofonseca commented 7 years ago

@mdavis332 , sorry for the delay! I'm getting your pings, but I haven't had much free time, unfortunately.

I apologize if I'm incorrect, but this is what I think is happening. (Sorry in advance if this doesn't make sense... I'm not very good at explaining things).

In your mock, you're returning $returned, which is a hashtable, just like the real Invoke-WebRequest would inside of New-HipChatUser.ps1 (which is exactly what we want).

If we take a look at New-HipChatUser.ps1, we're returning a similar variable, called $Call, which includes the same hashtable that you're returning in $returned. $returned and $call both have a key called StatusCode, so $Returned.StatusCode and $Call.StatusCode are the same ('201').

In our test, we're running New-HipChatUser and storing the result in a variable, called $Result. At the end of New-HipChatUser.ps1, we're not returning $Call (the full hashtable), we're returning $Call.StatusCode (just one key), so the $Result should only be '201'.

At the end of the test, you're attempting to call $Result.StatusCode.ToString(), but .StatusCode doesn't exist, because New-HipChatUser.ps1 is already returning the .StatusCode, not the full hashtable. Changing your test to just look for $Result.ToString() will fix this.

When I tried doing that myself, I was able to get the test to pass. This is also confirmed because the API key in the test is fake, so there's no way the function would actually be making an API call. Your mock seems to be working perfectly!

Hope this all makes sense! Sorry for the short story.

cofonseca commented 7 years ago

@mdavis332 Ah, I think you were right. My apologies! No worries on the delays - I'm still new to the process myself so I'm glad we're both learning.

I went ahead and resolved the conflict. This looks good. I'll merge it in. Thanks for your help!