I'm writing unit test for core application. Im trying to check, that my class throws exception. But ExpectedException attribute throws compile exception:
Error CS0246 The type or namespace name 'ExpectedException' could not be found (are you missing a using directive or an assembly reference?) EventMessagesBroker.Logic.UnitTests..NETCoreApp,Version=v1.0
My code:
[Fact] [ExpectedException(typeof(MessageTypeParserException))] public void TestMethod1_Error_twoMathces() { var message = "some text"; var parser = new MessageTypeParser(); var type = parser.GetType(message); Assert.Equal(MessageType.RaschetStavkiZaNalichnye, type); }
so, is there any correct way to achieve that?
Sometimes, you want to write tests and ensure they run against several target application platforms. The xUnit.net test runner that we've been using supports . NET Core 1.0 or later, . NET 5.0 or later, and .
To write a test you simply create a public method that returns nothing and then decorate it with the Fact attribute. Inside that method you can put whatever code you want but, typically, you'll create some object, do something with it and then check to see if you got the right result using a method on the Assert class.
It's an open source unit testing tool for . Net framework that's compatible with ReSharper, CodeRush, TestDriven.Net, and Xamarin. You can take advantage of xUnit.Net to assert an exception type easily.
Use Assert.Throws
on code where exception expected:
[Fact] public void TestMethod1_Error_twoMathces() { var message = "some text"; var parser = new MessageTypeParser(); Assert.Throws<MessageTypeParserException>(() => parser.GetType(message)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With