Passing a Failure

5 posts, 0 answers
  1. Matthew
    Matthew avatar
    16 posts
    Member since:
    Jun 2014

    Posted 30 Jun 2014 Link to this post

    In some of my unit tests I run the "unhappy path" to show a failure result as another test. My question is when using a fluent Assert such as:

            [TestMethod]
            public void U05WILLFAILShouldOccursNever()
            {
                var alpha = Mock.Create<IStingPatrol>();

                alpha.Submit();

                Mock.Assert(() => alpha.Submit(), Occurs.Never());
                
            }

    how do I pass this failure? I know it will fail, that is the purpose of this. but how do I make it so it does not come up as a failure when I run my tests? 
  2. Matthew
    Matthew avatar
    16 posts
    Member since:
    Jun 2014

    Posted 02 Jul 2014 in reply to Matthew Link to this post

    I know that on a normal Assert if you are expecting your result and your "actual" to not be equal then you can just use the Assert.AreNotEqual... to say pass when they are not equal.

    What about when you use Mock.Assert? I know that they code above will not pass because it suppose to occur never, but it does because I want to test the unhappy path of this simple test. 

    how do I make a failure pass when I want it to fail?
  3. DevCraft R3 2016 release webinar banner
  4. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 03 Jul 2014 Link to this post

    Hello Matthew,

    Thank you for contacting.

    With the Microsoft Unit Test Framework, you can use the [ExpectedException()] attribute for that purpose.

    For example, in your case you can use the following:
    [TestMethod]
    [ExpectedException(typeof(AssertFailedException))]
    public void U05WILLFAILShouldOccursNever()
    {
        var alpha = Mock.Create<IStingPatrol>();
     
        alpha.Submit();
     
        Mock.Assert(() => alpha.Submit(), Occurs.Never());
    }

    I hope this helps.

    Regards,
    Kaloyan
    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.

     
  5. Matthew
    Matthew avatar
    16 posts
    Member since:
    Jun 2014

    Posted 03 Jul 2014 in reply to Kaloyan Link to this post

    Thank you. It worked exactly the way I need it to.
  6. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 07 Jul 2014 Link to this post

    Hi Matthew,

    I am glad I could help.

    Further, you can also check this article about why using the MSTest ExpectedException attribute is not recommended. Instead you can benefit from the already developed Assert.Throws functions of other testing frameworks (NUnit for example), or even develop an exception handler by your own.

    I hope this helps.

    Regards,
    Kaloyan
    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.

     
Back to Top
DevCraft R3 2016 release webinar banner