c# - Is it possible to write a unit test for the method that has, 1)Return type void, 2)access specifier private or protected? -
c# - Is it possible to write a unit test for the method that has, 1)Return type void, 2)access specifier private or protected? -
is possible write nunit framework unit test class have method void homecoming type , access specifier protected or private in classic asp.net. allow me clear because im beginner in unit testing.
update:this void function in case means, how can write unit test this?
public void function(textbox txtbox, string result) { if (result.trim().tostring() == " ") { txtbox.text = ""; } else { txtbox.text = result; } }
i think you're asking 2 questions:
is possible write nunit framework unit test class have method void homecoming type? possible, in way have know went fine during execution of method. example, if throws exception, test failed, otherwise considered succeeded. furthermore, can check other properties if side effect of method has changed these properties.
is possible write nunit framework unit test class access specifier protected or private in classic asp.net? not mutual practise unit test private , protected members. however, if need it, can done reflection, can utilize internal
instead of private. in assemblyinfo.cs, utilize internalsvisibleto
attribute this:
[assembly:internalsvisibleto("unittestassembly")]
only unittestassembly classes can access internal members. members remain hidden other classes. unittestassembly must name of unit test project/assembly. not work when assembly strong named; in case have specify more. this link might help in case.
update:...how can write unit test this? declare internal function in class function
defined:
internal string gettextboxtext(textbox txtbox) { homecoming txtbox.text; }
now can write unit tests:
... yourclassinstance.function(mytxtbox, "somestring"); assert.istrue(yourclassinstance.gettextboxtext(mytxtbox) == "somestring"); ... yourclassinstance.function(mytxtbox, " "); assert.istrue(yourclassinstance.gettextboxtext(mytxtbox) == "");
but happens when specify null textbox?
yourclassinstance.function(null, " "); // throw exception assert.istrue(yourclassinstance.gettextboxtext(mytxtbox) == "");
how must handled? can verify in unit test.
c# asp.net unit-testing nunit
Comments
Post a Comment