c# - Verify that a base protected method is called with Moq 3.1 -
i have class inherits abstract base class. trying verify specified protected method in base class called twice , i'd verify parameters passed specific values (different each call).
i hoping i'd able use protected either expect or verify, seemingly i've missed can done these methods.
is i'm attempting possible moq?
update: example of i'm trying do:
class mybase {     protected void somemethodthatsapainforunittesting(string var1, string var2)     {         //stuf file systems etc that's hard unit test     } }  class classiwanttotest : mybase {     public void iwanttotestthismethod()     {         var var1 = //some logic build var 1         var var2 = //some logic build var 2         somemethodthatsapainforunittesting(var1, var);     } } essentially want test way variables var1 , var2 created correctly , passed somemethodthatsapainforunittesting, want mock out protected method, verify called @ least once , parameters passed correctly. if calling method on interface trivial, i'm coming unstuck protected method.
i can't change design it's brown field development , i'm not class calling method.
no it's isn't possible.
tools moq , rhino mocks work magic generating subclasses of types want mock @ run time. generate "verify" , "expect" logic overriding member (in case of virtual members) or implementing (in case of interface) , inserting code inspect or record passed arguments , return pre-canned response.
so can around this? firstly if alter base method virtual allow test method creating test harness so:-
class classiwanttotestharness : classiwanttotest {     public string arg1 { get; set; }     public string arg2 { get; set; }     public int callcount { get; set; }      protected override void somemethodthatsapainforunittesting(var1, var2) {         arg1 = var1;         arg2 = var2;             callcount++;     } }  [test] public void classiwanttotest_doeswhatitssupposedtodo() {     var harness = new classiwanttotestharness();     harness.iwanttotestthismethod();     assert.areequal("whatever", harness.arg1);     assert.areequal("it should be", harness.arg2);     assert.isgreaterthan(0, harness.callcount); } if it's not possible alter base class @ due code being horribly brownfield don't want shoes dirty, wrap method in classiwanttotest protected virtual method , perform same trick on instead. moq have support overriding protected virtual members (see miscellaneous section here) - though prefer manual test harnesses in case.
Comments
Post a Comment