This is a migrated thread and some comments may be shown as answers.

Testing kickstart help

1 Answer 105 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Tommy
Top achievements
Rank 1
Tommy asked on 30 Sep 2014, 02:57 PM
Hi there,

I'm new to JustMock and I'm looking into writing unit tests. Below i have copied the code i'm trying to test and will also copy what i have so far for unit testing. Looking at the test covarage within Visual Studio i aren't testing the catch exceptions and i don't know how to do this. Most my tests are structured this way and any help to get me moving again will be appreciated.

[HttpGet]
 [Route("api/DealBuilder/GetSession")]
 public IHttpActionResult NewVersion(string sessionId)
 {
     try
     {
         if (!String.IsNullOrEmpty(sessionId))
         {
             try
             {
                 return Ok(dealbuilderRepository.GetTemplateAndXml(new Guid(sessionId)));
             }
             catch (Exception ex)
             {
                 return InternalServerError(ex);
             }
         }
         else
         {
             return BadRequest("No Session ID was passed.");
         }
 
     }
     catch (Exception ex)
     {
         // TODO: logger.Error(ex.Message, ex);
         return InternalServerError(ex);
     }
 }

This is what i have so far for tests on my code above.

[TestMethod]
  public void NewVersion()
  {
      var repo = Mock.Create<IDealBuilderRepository>();
 
      var controller = new DealBuilderController(repo);
 
      Mock.Arrange(() => repo.Equals(Arg.IsAny<String>()));
 
      var result = controller.NewVersion(Arg.IsAny<String>());
 
      Mock.Assert(repo);
  }
 
 
  [TestMethod]
  public void GetNewVersionFail()
  {
      var repo = Mock.Create<IDealBuilderRepository>();
 
      var controller = new DealBuilderController(repo);
 
      Mock.Arrange(() => repo.Equals(Arg.IsAny<Guid>())); // .Returns(null);
 
      var result = controller.NewVersion(new Guid().ToString()) as InternalServerErrorResult ;
 
      Mock.Assert(typeof(InternalServerErrorResult));
  }



Thanks again,

Tommy

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 01 Oct 2014, 09:46 AM
Hi Tommy,

The direction that you're taking looks generally correct.

One problem that I see is that you're using Mock.Assert without first etablishing expectations. Mock.Assert is not a substitute for the Assert class coming from the testing framework itself. It only asserts that all expectations on an object are met (e.g. MustBeCalled(), Occurs(), InOrder()).

In your case it would make sense to write the end of the second test like this:
var result = controller.NewVersion(new Guid().ToString()) as InternalServerErrorResult;
Assert.IsNotNull(result);

For an excellent introduction to TDD and AAA, I'd like to refer you to the "30 days of TDD" series by James Bender. An introduction to mocking in general and JustMock in particular starts at day 11.

I hope that you have a pleasant experience mocking.

Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
General Discussions
Asked by
Tommy
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or