Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrectly implement Unit Test
    primarykey
    data
    text
    <p>I'm practicing on writing unit tests for the first time, and I have some questions. I'll start of by explaining what I'm trying to test. </p> <p>I would like to test a method which looks like this: </p> <pre><code>public bool IsAdmin(HubCallerContext hubCallerContext) { return hubCallerContext.User.IsInRole("admin"); } </code></pre> <p>The method is implemented in a class <code>UserService</code>, which is connected to a interface <code>IUserService</code>.</p> <p>I'm trying to create 2 tests: </p> <ul> <li>One with a HubCallerContext which is in the role of "admin" and will assert true.</li> <li>One with a HubCallerContext which is in the role of "user" and will assert false.</li> </ul> <p>I've created a new class library in my solution, where I've refrenced the project I'm testing. I've installed NUnit and Moq, and created a test class which looks like this:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using ChatProj; using NUnit.Framework; using ChatProj.Controllers; using Moq; using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR.Hubs; using ChatProj.DAL; using ChatProj.Service_Layer; using System.Threading.Tasks; namespace ChatProj.Tests { [TestFixture] public class Class1 { [SetUp] public void Setup() { } [Test] public void IsAdmin_CalledByAdmin_ReturnTrue() { UserService userService = new UserService(); bool result = userService.IsAdmin( ? ); Assert.IsTrue( result, "Something is wrong." ); } [Test] public void IsAdmin_CalledByUser_ReturnFalse() { UserService userService = new UserService(); bool result = userService.IsAdmin( ? ); Assert.IsFalse( result, "Something is wrong." ); } } } </code></pre> <p>Here I start to get confused. (I've marked the parameters of the IsAdmin calls with "?" because I'm not sure what to put there.)</p> <p>I've read about mocks, stubs, fakes and dummies, but the definitions are to abstract for me to really grasp. I've found these definitions for example:</p> <pre><code> - Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists. - Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example). - Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'. - Mocks are objects pre-programmed with expectations which form a specification of the calls they are expected to receive. </code></pre> <p>As I've designed my test class, I would need some sort of substitution for my HubCallerContext. This is assuming I'm testing the "IsAdmin" method the right way.</p> <p>So my questions are: </p> <ul> <li><p>Am I testing the "IsAdmin" method in a good way? </p></li> <li><p>How would I practically make the tests work? Do I use a mock, and in that case, could you show how I would implement that, or point me in the right direction? <a href="https://github.com/SignalR/SignalR/blob/master/src/Microsoft.AspNet.SignalR.Core/Hubs/HubCallerContext.cs" rel="nofollow" title="Here is how the HubCallerContext works for refrence.">Here is how the HubCallerContext works for refrence.</a></p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload