Testing kickstart help

2 posts, 0 answers
  1. Tommy
    Tommy avatar
    4 posts
    Member since:
    Jun 2014

    Posted 30 Sep 2014 Link to this post

    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
  2. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 01 Oct 2014 Link to this post

    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.

     
  3. DevCraft R3 2016 release webinar banner
Back to Top