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

Passing a Failure

4 Answers 22 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 30 Jun 2014, 02:30 PM
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? 

4 Answers, 1 is accepted

Sort by
0
Matthew
Top achievements
Rank 1
answered on 02 Jul 2014, 12:39 PM
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?
0
Kaloyan
Telerik team
answered on 03 Jul 2014, 11:02 AM
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.

 
0
Matthew
Top achievements
Rank 1
answered on 03 Jul 2014, 12:47 PM
Thank you. It worked exactly the way I need it to.
0
Kaloyan
Telerik team
answered on 07 Jul 2014, 07:57 AM
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.

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