c# - How to unit test task from private method -


i have method (some code removed):

try {    var task = task.factory.startnew(validateaccounts, _token); } catch (aggregateexception) {    _tokensource = new cancellationtokensource();    _token = _tokensource.token; } 

validateaccounts private method withing same class. test called , able mock task , try exception thrown , without it.

as others have mentioned, have careful when treading fine line of what-to-test vs. what-not-to-test. can lead brittle tests coupled implementation.

that being said, absolutely see utility in doing you're trying do. testing exception handling alone worth asking question.

the problem rooted in use of static object (task.factory). static objects/methods notorious impeding testability. solution problem same used decoupling any type of static object - introducing seam.

you using static object kick off, , return reference to, task. can define role interface for. might like:

public interface itaskfactory {     task startnew(action action); } 

with production implementation looks like:

public class tpltaskfactory : itaskfactory {     public task startnew(action action)     {         return task.factory.startnew(action);     } } 

you use dependency injection supply sut mock implementation of interface, gives capability you're looking for.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -