c# - How to write unit test case for BadRequest? -


i want write unit test cases following code

homecontroller.cs

[httppost]         [actionname("createdemo")]         public async task<ihttpactionresult> createdemo([frombody] myrequest request)         {             if (request == null)             {                                     return badrequest("request can not null");             }             if (request.myid == guid.empty)             {                 return badrequest("myid must provided");             }         } 

i tried following not correct way guess so

 [testmethod]         public async task nullcheck()         {             try             {                 var controller = new homecontroller();                 var resposne = await controller.createdemo(null);                 assert.areequal(); // not sure put here             }             catch (httpresponseexception ex) //catch not hit             {                 assert.istrue(                      ex.message.contains("request can not null"));             }          } 

each unit test shall test 1 requirement or concern. method implements 2 requirements:

1) if request null, return badrequesterrormessageresult object predefined error message. 2) if request's myid property empty guid, return badrequesterrormessageresult object predefined error message.

this means should have 2 unit tests:

[test] public async task createdemo_returns_badrequesterrormessageresult_when_request_is_null() {    // arrange    var controller = new homecontroller();     // act    var response = await controller.createdemo(null);     // assert    assert.isinstanceof<badrequesterrormessageresult>(response);    assert.areequal("request can not null", response.message); }  [test] public async task createdemo_returns_badrequesterrormessageresult_when_request_id_is_empty_guid() {    // arrange    var controller = new homecontroller();    var request = new myrequest(guid.empty);     // act    var response = await controller.createdemo(request);     // assert    assert.isinstanceof<badrequesterrormessageresult>(response);    assert.areequal("myid must provided", response.message); } 

you can go further , split each of these tests 2 1 test return object of expected type , validates returned object state expected (e.g. message string expected). way have single assert per test.

side notes:

you tagged question nunit tag provided code uses framework. in example though, use [testmethod] attribute comes microsoft unit testing framework. if want use framework you'd have make changes e.g. replace assert.isinstanceof assert.isinstanceoftype.

i assumed guid passed myrequest via constructor assigns myid.

i not coming web world found badrequest method has overload returns badrequesterrormessageresult if string passed argument.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

python 3.5 - Pyqtgraph string in x tick -