Mock.Initialize

2 posts, 0 answers
  1. Jose
    Jose avatar
    5 posts
    Member since:
    Apr 2011

    Posted 13 May 2011 Link to this post

    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?

  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 16 May 2011 Link to this post

    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
  3. DevCraft R3 2016 release webinar banner
Back to Top