0

I have following in my controller file:

public ActionResult Index()
{
    var model = new ClientGroupIndexViewModel()
    {
        AddScanUrl = Url.RouteUrl<ClientGroupsController>(c => c.CreateNewScan(null, null)),
        //other similar stuff
    };

    return View("ClientGroupsIndex", model);
}


public JsonResult CreateNewScan(ClientGroup clientGroup, Version version)
{
    //do something
    return Json(new{Message = "created new scan", Success = true});
}

I've to write Unit test case for CreateNewScan. How can this be done (preferably using built-in testing framework in VS)? Any pointers in this regard appreciated.

ppalms
  • 394
  • 2
  • 7
  • 19
Maxsteel
  • 1,922
  • 4
  • 30
  • 55
  • http://stackoverflow.com/a/8563657/2436549 – Zafar Jul 30 '14 at 13:41
  • Using the link @Zafar posted, you might want to check the Success value in your test. – Darek Jul 30 '14 at 13:47
  • @Darek Trying to take help of the answer in the posted link but I'm a total newbie and couldn't understand much. Any further help is deeply appreciated. – Maxsteel Jul 30 '14 at 13:48
  • possible duplicate of [Unit Testing MVC Controllers](http://stackoverflow.com/questions/8562888/unit-testing-mvc-controllers) – krillgar Jul 30 '14 at 14:21

1 Answers1

1

This should work, although for some reason on my workstation throws an error.

    [TestMethod]
    public void CreateNewScan()
    {
        HomeController controller = new HomeController();
        ClientGroup clientGroup = new ClientGroup(){ 
            //some property initializers
        };
        JsonResult result = controller.CreateNewScan(clientGroup, new Version(1, 0));
        dynamic data = result.Data;
        Assert.IsTrue(data.Success);
    }

Please try and let me know. My PC has been acting up recently.

UPDATE

The PC was "acting up" because when using dynamic type, you must allow the test project to see the internals of your web application. So in your AssemblyInfo.cs of the web application, make sure you add this line:

[assembly: InternalsVisibleTo("WebApplication1.Tests")]

where WebApplication1.Tests is the assembly name of your test project.

Community
  • 1
  • 1
Darek
  • 4,687
  • 31
  • 47
  • Thanks for the efforts. Sorry if this is a stupid question but it also asks for [UrlToTest()]. I know that there are some url routing here. The one I see in the browser is not acceptable. How do I fnd right URL to test? – Maxsteel Jul 30 '14 at 14:24
  • Do you mean how to test Index()? – Darek Jul 30 '14 at 14:29
  • Never mind, was a stupid question anyway. This works :) Thanks a lot. – Maxsteel Jul 30 '14 at 14:30