chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
May i know how to test the doSomething() under class1? How to write the expectCall arguments? Is it need to mock all the instances of this class? Thanks....

coding to be tested

public class class1
{
private class class2;

public class1
{
class2 = new class2();
}

public void doSomething()
{
class2.start(new class2.StartEventHandler(this.startDoSomething));
}

private void startDoSomething(int a){}
}

public class class2
{
public delegate void StartEventHandler(int a);

private StartEventHandler sEHandler;

public class2{}

public void start(StartEventHandler seh)
{
sEHandler = seh;

----------------------------------
---------------------------------
----------------------------------
}

}
asked by clteh9 (5.3k points)

8 Answers

0 votes
Hi,
If anybody feels like answering, please do so.
We cannot answer these kinds of questions.

But I can give you a pointer.
1. Think of the test scenario that you are testing.
2. Setup that scenario.
3. Mock anything that needs mocking.
4. Run the scenario.
5. Test the results.

You can see more patterns in our Articles

If you try this but couldn't find a way to mock. Please write the test class and what you are testing and we and other people might help.
answered by scott (32k points)
0 votes
answered by scott (32k points)
0 votes
Below is the test case. There is somethign wrong with the expect arguments. Anybody can help to give some ideas about it? Thanks for helping.

[Test]
public void test()
{
MockManager.Init();
Mock mockClass2 = MockManager.MockAll(typeof(class2));

class1 c1 = new class1();

mockClass2.ExpectCall("start").Args(Check.IsMock(mockClass2));

c1.doSomething();
}
answered by clteh9 (5.3k points)
0 votes
this is the exception caught.
Could not load type <unknown> from assembly RcocClientTest, Version=1.0.2396.20779, Culture=neutral, PublicKeyToken=null.
answered by clteh9 (5.3k points)
0 votes
Although there are many syntax mistakes in your code (which is a sign that this might not be the real code problem). When I fix them I get the correct result:
TestCase 'TestUsingTypeMock.Test35.Topic260'
failed: TypeMock.VerifyException : 
TypeMock Verification: Call to TestUsingTypeMock.class2.start() Parameter: 1
    passed object [TestUsingTypeMock.class2+StartEventHandler] is not controlled by expected mock.


Suppose that you want to check that the correct event has been passed. You can do that as follows, using a Custom Checker

[Test]
public void Topic260()
{
    MockManager.Init();
    Mock mockClass2 = MockManager.MockAll(typeof(class2));

    class1 c1 = new class1();

    mockClass2.ExpectCall("start").Args(Check.CustomChecker(CheckEventHandlerPassed,c1));

    c1.doSomething();
    MockManager.Verify();
}
// Here we will check that the correct method is send
private bool CheckEventHandlerPassed(ParameterCheckerEventArgs e)
{
    Delegate d = (Delegate) e.ArgumentValue ;
    class1 c1 = (class1)e.ExpectedValue;
    if (object.ReferenceEquals(c1, d.Target) &&
        d.Method.Name == "startDoSomething")
    {
        return true;

    };
    return false;
}


Of course, perhaps a better way is to check that our event handler is called (I don't know the code so Im guessing)

[Test]
public void Topic260()
{
    MockManager.Init();
    Mock mockClass1 = MockManager.Mock(typeof(class1));
    mockClass1.ExpectCall("startDoSomething");

    class1 c1 = new class1();
    c1.doSomething();
    c1.MakeClass2ToFireEvent();

    // make sure out event was called once.
    MockManager.Verify();
}
answered by scott (32k points)
0 votes
Method 'ClassLibrary2.Test.CheckEventHandlerPassed(TypeMock.ParameterCheckerEventArgs)' referenced without parentheses

I did exactly same as you. However i get the error above when build. can you tell me why i can get such error? I did refer the example in this website, it also call the function as u called without arguments.
answered by clteh9 (5.3k points)
0 votes
i found the solution already.

mockClass2.ExpectCall("start").Args(Check.CustomChecker(new ParameterCheckerEx(CheckEventHandlerPassed),c1));
answered by clteh9 (5.3k points)
0 votes
Ahh, the benefits of working with .NET 2.0 8)
answered by scott (32k points)
...