c# - Which type of exception should I use in ExpectedExceptionAttribute while unit testing ? -


I am working on unit testing. I want to use ExpectedExceptionAttribute.

I have an employee class that has a username property on which I have used indexing, so the user name must be unique.

The code is given below

Transfer of Public Class Employee: EntityTypeConfiguration & lt; Employee & gt; {Public Employee Convention () Property (x = & gt; x.FirstName) .IsRequired (). HasMaxLength (50); this. Property (T = & gt; T. Usernam). Column annotation (index annotation.NotationName, new index annotation ("IX_UserName", 1) {iicune = true}); }}

Now, consider the unit testing code below. [TestMethod] [Expected Exception (Typfix (System.Data. SQLLient.SqlException), "Username Duplication is not allowed")] Public Zero Insert_EmployeeWithSameUsername_Fails () {... ... .. ..}

I have used SqlException, but it does not work, it still throws an error ... what type of exception to me in the unit test code needed?

From

:

If the thrown exception is inherited then The examination will fail with the expected exception.

It seems that you are actually receiving an exception that comes from SqlException .

Use it instead, or better yet, if you can use xUnit / NUnit.

try {// code} hold (from SqlException) {} hold (exception e) {Assert.Fail (string.Format ("caught {0} unexpected exception of type: {1 } ", E. GateType (), E. Message)); }


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -