.net - Problems with Ref Keyword in MOQ -
hoping can me.
i trying test method (outer method) has dependency class invokes method (inner method). inner method takes boolean ref parameter, , problem far been unable control boolean ref parameter.
(note-the code shown below has been written purpose of illustrating problem , not code looks like).
the moq documentation here - https://github.com/moq/moq4/wiki/quickstart gives overview of dealing ref/out parms have not found helpful.
i tried example works (which found here - assigning out/ref parameters in moq)
public interface iservice { void dosomething(out string a); } [test] public void test() { var service = new mock<iservice>(); var expectedvalue = "value"; service.setup(s => s.dosomething(out expectedvalue)); string actualvalue; service.object.dosomething(out actualvalue); assert.areequal(actualvalue, expectedvalue); }
but when tried code want run cannot work. included below.
the interface
public interface igetvalue function getvalue(byref myboolofinterest boolean) single end interface
the code wish test
public class classtobetested dim isproblem boolean public function passcomparisonvalues(m_getvaluegetter igetvalue) boolean dim dpbar single = m_getvaluegetter.getvalue(isproblem) return isproblem end function end class
the code have written test below (note - different project).
public void methodtotest() { // arrange // system under test classtobetested myclasstobetested = new classtobetested(); // construct required mock mock<igetvalue> mymock = new mock<igetvalue>(); bool isproblem = true; mymock.setup(t => t.getvalue(ref isproblem)); // act isproblem = myclasstobetested.passcomparisonvalues(mymock.object); // assert assert.that(isproblem, is.equalto(true)); }
what want able control contents of isproblem in classtobetested, , finding not happening. contains false no matter do.
hope can help.
what make isproblem
protected in classtobetested
. unit tests inherit classtobetested
, set value of isproblem
expected value.
class classtobetested { protected bool isproblem; // code here ... } class testableclasstobetested : classtobetested { public testableclasstobetested(bool isproblemvalue) { isproblem = isproblemvalue; } } public void methodtotest() { // arrange // system under test bool isproblem = true; classtobetested myclasstobetested = new testableclasstobetested(isproblem); // code here ... }
Comments
Post a Comment