This is a migrated thread and some comments may be shown as answers.

Mock.Initialize

1 Answer 67 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jose
Top achievements
Rank 1
Jose asked on 13 May 2011, 05:36 PM
I am having similar issues as those in this thread:
http://www.telerik.com/community/forums/justmock/general-discussions/mocks-failing-to-work-correctly-in-large-test-runs.aspx

Basically, I have some unit tests that work fine when i run them alone, together however, the first test passes, then the rest fail. It seems as if the first tests mocks are successfully intercepted, and then the subsequent tests mocks are not.

The solution it seems is the Mock.Initialize function. Does Mock.Initialize apply to class methods that are not static? If so, is there an example I could take a look at?

1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 16 May 2011, 04:05 PM
Hi Jose,
Thanks again for sending the post. Yes you can use Mock.Initalize for instance methods as well. Like let's take the following example:

[TestFixtureSetUp]
 public void Initialize()
 {
     Mock.Partial<FileInfo>().For((x) => x.Delete());
 }


Then, inside the test method i did the following to see if everything worked as expected.

[TestMethod]
public void ShouldAssertSetupCalls()
{
    var filename = this.GetType().Assembly.ManifestModule.FullyQualifiedName;
    var fi = new FileInfo(filename);
    bool called = false;
    Mock.Arrange(() => fi.Delete()).DoInstead(() => called = true);
    fi.Delete();
    Assert.True(called);
}


In addition, you can find similar ones under %Program_Files%\Telerik\JustMock\Examples folder in Elevated\MsCorlibFixture.cs for c# examples.

Hope the information is useful.

Kind regards,
Ricky
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
Jose
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or