I am trying to figure out how to mock a function in another class to the one that i am testing

37 views
Skip to first unread message

Paul Daulby

unread,
Dec 5, 2013, 9:35:10 AM12/5/13
to moq...@googlegroups.com
The function i am testing calls a function in another class, which calls another function is the 'another class' which i need to mock out.

I can't figure out how to do this.
 
here is a stackoverflow post I made about it. http://stackoverflow.com/questions/20402682/mocking-a-function-called-by-the-function-you-are-unit-testing

oh, also, i apologise if this isn't a good place to ask questions about how to use moq.

Zelalem

unread,
Dec 6, 2013, 3:40:59 AM12/6/13
to moq...@googlegroups.com
This is what i see from your link above:

For example you are testing getSomeString.

public class Class1
{
    public Class1() {}
    public string getSomeString(string input)
    {
        //dosomething
        OtherClass class = new OtherClass();
        return class.getData();
    }
}

public class OtherClass
{
    public Otherclass() {/*some data*/}    
    public string getData()
    {
        //do something
        string aString = getAString(Input);
        //do something
    }

    public virtual string getAString(string input);
    {
        //do something to be mocked
        return somethingToBeMocked;
    }
}

So you need the unit test to mock out getAString. How do you do this?

I have tried just mocking getAString and hoping the test uses the mocked value, but that doesn't work.

I have tried mocking Class1 and setting the unit test class1 to be the mocked object, but i could not figure out how to make the mocked getAString passed into the mocked class1.

I cannot figure out what to do.

________

You are trying to test getAString method and you are trying to mock getAString method? getAString method is the method you are trying to test. You shouldn't mock it. Mocking should be applied on dependencies. For instance, had getAString method in class

OtherClass calls a method or property on another class such as Class1, that is where you need to apply 
mocking; to have full control of what the object the method depends/calls return. 
If i missed your question, please explain it with example and specify which method you want to test.


Thanks and have a great day!



--
--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Moq Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to moqdisc+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Paul Daulby

unread,
Dec 6, 2013, 8:13:08 AM12/6/13
to moq...@googlegroups.com


On Friday, 6 December 2013 08:40:59 UTC, Bsharper wrote:

You are trying to test getAString method and you are trying to mock getAString method? getAString method is the method you are trying to test. You shouldn't mock it. Mocking should be applied on dependencies. For instance, had getAString method in class

OtherClass calls a method or property on another class such as Class1, that is where you need to apply 
mocking; to have full control of what the object the method depends/calls return. 
If i missed your question, please explain it with example and specify which method you want to test.


Thanks and have a great day!


I am trying to the the 'getSomeString' method. Sorry if i didn't make that clear.

But for this example, the 'getAString' will not work in unit testing (because it uses a database that doesn't exist or website that isn't set up yet or any other reason) so needs to be mocked

Daniel Cazzulino

unread,
Dec 6, 2013, 9:34:12 AM12/6/13
to moqdisc
The answers on SO are spot-on on this.


/kzu

--
Daniel Cazzulino


--
Reply all
Reply to author
Forward
0 new messages