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



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

i working on unit testing. want utilize expectedexceptionattribute.

i have employee class contain username property, on have used indexing, username should unique.

the code below.

public class employeeconfigration : entitytypeconfiguration<employee> { public employeeconfigration() { this.property(x => x.firstname).isrequired().hasmaxlength(50); this.property(t => t.username).hascolumnannotation(indexannotation.annotationname, new indexannotation(new indexattribute("ix_username", 1) { isunique = true })); } }

now, consider below code of unit testing.

[testmethod] [expectedexception(typeof(system.data.sqlclient.sqlexception), "username duplication not allowded")] public void insert_employeewithsameusername_fails() { ... ... ... }

i have used sqlexception, not working, still throws error... kind of exception should used in unit testing code?

from msdn:

the test fail if thrown exception inherits expected exception.

it looks you're getting exception derives sqlexception.

use instead of attribute, or improve yet, utilize xunit / nunit if can.

try { //code } grab (sqlexception se) { } grab (exception e) { assert.fail( string.format( "unexpected exception of type {0} caught: {1}", e.gettype(), e.message ) ); }

c# unit-testing

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -