RSS

Tag Archives: unit test

Mocking Generic Method with NSubstitute

The generic method:


public interface ICacheService
{
    T Get<T>(string id);
}

User object in NSubstitute to mock the generic method.


private ICacheService MockICacheService()
{
    var service = Substitute.For<ICacheService>();

    // Return Models
    object returnForAny = null;

    // Mock
    service.Get<object>(Arg.Any<string>()).ReturnsForAnyArgs(returnForAny);

    return service;
}

Advertisements
 
Leave a comment

Posted by on September 13, 2017 in General

 

Tags: ,

Return Null in NSubstitute Mock Framework

The API is not intuitive, at least not for me. So, I have to figure out how to return null with this mocking framework I have never used before.

var service = Substitute.For<ISomethingService>();

// Mock
service.Get<IEnumerable<SomeDomain>>(Arg.Any<string>()).ReturnsForAnyArgs(x => null);

Normally, I would just to .ReturnsForAnyArgs(null), not the case with NSubstitute.

 
1 Comment

Posted by on May 27, 2015 in General

 

Tags: ,

Unit Testing WIF’s ClaimsPrincipalPermission.CheckAccess

WIF 4.5 has ClaimsPrincipalPermission.CheckAccess method, very useful to check user’s authorization. You can use this as method call or attribute.

// Imperative method call
using System.IdentityModel.Services;
public ActionResult Index()
{
    ClaimsPrincipalPermission.CheckAccess("foo", "bar");

    return View();
}

// Attribute
[ClaimsPrincipalPermission(SecurityAction.Demand, Operation="foo", Resource="bar")]
public ActionResult ViewFoobar()
{
    return View();
}

Either way, how do we unit test this? My approach is to first abstract out ClaimsPrincipalPermission and create a new wrapper class that will be injected to the dependent class.

Abstract Out

using System.IdentityModel.Services;

public class ClaimsPrincipalWrapper : IClaimsPrincipalWrapper
{
    public void CheckAccess(string resource, string action)
    {
        ClaimsPrincipalPermission.CheckAccess(resource, action);
    }
}

Dependency Injection

using System.IdentityModel.Services;

public class HomeController : Controller
{
    private readonly IClaimsPrincipalWrapper _ClaimsPrincipalWrapper;

    public HomeController(IClaimsPrincipalWrapper claimsPrincipalWrapper)
    {
        _ClaimsPrincipalWrapper = claimsPrincipalWrapper;
    }

    public ActionResult Index()
    {
        _ClaimsPrincipalWrapper.CheckAccess("foo", "bar");

        return View();
    }
}

Unit Test

[TestMethod]
public void TestIndex()
{
    // Arrange
    var _claimsPrincipal = new Mock<IClaimsPrincipalWrapper>();
    _claimsPrincipal.Setup(m => m.CheckAccess(It.IsAny<string>, It.IsAny<string>));
    var _controller = new HomeController(_claimsPrincipalMock.Object);

    // Act
    var _result = _controller.Index() as ViewResult;

    // Assert
    Assert.IsTrue(_result.View != null);
}
 
Leave a comment

Posted by on May 4, 2015 in General

 

Tags: , , , , , , ,

Visual Studio 2012: Debugging Unit Tests Doesn’t Work

There’s a problem in Visual Studio 2012 where debugging unit tests doesn’t work. This often happens when you enable code coverage.

The solution is fairly simple, un-check any selected test setting file under Test > Test Settings.

This behavior is expected, at least according to Microsoft, they just don’t support debugging for code coverage.

 
Leave a comment

Posted by on May 1, 2015 in General

 

Tags: , ,

Guide to Testable Code

If you haven’t already, go learn unit testing and write testable code. I promise you, your code will look much better and engineer-like. And not just the look, more importantly it reflects into your application, more robust, higher quality, SOLID. If that’s not enough, it even reflects into yourself and your career, you look more professional and not some ‘just make it work’ developers.

Good guys (and smart, of course) at Google provide easy to understand and simple to follow guide for testable code.
http://misko.hevery.com/code-reviewers-guide/

The e-book is free as well: http://misko.hevery.com/attachments/Guide-Writing%20Testable%20Code.pdf

 
Leave a comment

Posted by on April 28, 2015 in General

 

Tags: ,

Good Unit Test Qualities

  • Repeatable
    Unit tests should be repeatable. If a test fails, it should always fail, if it succeed, it should always be.
    Good unit tests also repeatable all the time, not succeed at certain time, but fail at other times.
  • Independent
    Each unit test should be independent, meaning it should not depend on other unit test for it to run.
    You should also be able to run each unit test by itself, or a select group of tests.
 
Leave a comment

Posted by on November 6, 2014 in General

 

Tags: ,

Basic Karma Commands

Following are some useful commands for Karma testing framework.

Display Karma version.

$ karma --version

Interactively create a karma config file (karma.conf.js).

$ karma init
 
Leave a comment

Posted by on September 2, 2014 in References

 

Tags: , ,

 
%d bloggers like this: