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

Issue with Static Mocking of Utility Class

5 Answers 191 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 31 May 2011, 03:06 PM
I have a Utility class that contains several static methods.  Originally I was setting up my static mock within an individual unit test but that was causing it to fail when run as a group of tests (I found the reason for that).  So to fix that issue I have moved the static setup to the ClassInitialize method. 

Here is the code....
Mock.SetupStatic<EagleUtility>();
           Mock.Arrange(() => EagleUtility.ValidateMessage(Arg.IsAny<byte[]>(), Arg.AnyInt, TowerTypes.Unified)).
             DoNothing().Returns(true).MustBeCalled();



This does not seem to be working.  When I call the test method that uses the mock and asserts that this and other methods are called it fails. I try debugging and see that the method in question is not being skipped as I expect it to.

Any suggestions?

5 Answers, 1 is accepted

Sort by
0
Chris
Top achievements
Rank 1
answered on 01 Jun 2011, 03:19 PM
Still working the issue on this end with no avail as of yet.  I did see that all the methods in the class are being "semi mocked" as I had forgotten to put calloriginal for behavior and expected some other methods to run normally that weren't.  I fixed that but this still doesn't seem to be working.
0
Chris
Top achievements
Rank 1
answered on 02 Jun 2011, 08:29 PM
Well still no luck nor any responses perhaps it will help if I show the calling code etc. so here we go (with minor edits where appropriate to protect comapny IP).


ClassInitialize Method
[ClassInitialize()]
       public static void MyClassInitialize(TestContext testContext)
       {
           Mock.SetupStatic<EagleUtility>(Behavior.CallOriginal);
           Mock.Arrange(() => EagleUtility.ValidateMessage(Arg.IsAny<byte[]>(), Arg.AnyInt, TowerTypes.Unified)).
             DoNothing().Returns(true).MustBeCalled();
       }

TestMethod
public void RecieveIncomingMessageTest()
        {
            var expectedTower = TestContext.DataRow["Tower"].ToString();
            var expectedEventDescription = TestContext.DataRow["EventDescription"].ToString().TrimStart('\r', '\n', ' ');
            expectedEventDescription = expectedEventDescription.TrimEnd(' ', '\n', '\r', '\0');
            var rawDataToUse = Convert.FromBase64String(TestContext.DataRow["RawData"].ToString());
            var called = false;
            var target = new UnifiedProtocolTranslator();
            ;
            int byteCount = rawDataToUse.Length;
            EagleIncomingMessageStatus expected = EagleIncomingMessageStatus.Complete;
            EagleIncomingMessageStatus actual;
 
            Mock.NonPublic.Arrange<bool>(target, "ProcessIncomingMessage", rawDataToUse, rawDataToUse.Length).DoNothing().Returns(true).MustBeCalled();
 
            Mock.NonPublic.Arrange(target, "CompileIncomingMessage").DoNothing().MustBeCalled();
            actual = target.RecieveIncomingMessage(rawDataToUse, byteCount);
 
            Mock.Assert(target);
  
 
            Assert.AreEqual(expected, actual);
        }


Method Being Tested.

public EagleIncomingMessageStatus RecieveIncomingMessage(byte[] message, int byteCount)
       {
           var messageType = (UnifiedIncomingMessageTypes) message[0];
           EagleIncomingMessageStatus returnValue = EagleIncomingMessageStatus.Continue;
           if (EagleUtility.ValidateMessage(message, byteCount, TowerTypes.Unified))
           {
               if (ProcessIncomingMessage(message, byteCount))
               {
                   CompileIncomingMessage();
                   returnValue = EagleIncomingMessageStatus.Complete;
               }
           }
 
           return returnValue;
       }


The test is failing on all runs for the test.  This is a data driven unit test pulling from an XML data source. So I believe I have to use Arg.IsAny<byte[]> in the Class Initialize method.

Failure Message is: "Test method UnifiedProtocolTest.UnifiedProtocolTranslatorTest.RecieveIncomingMessageTest threw exception: 
Telerik.JustMock.MockAssertionException: Setup contains calls that are marked as "MustBeCalled" but actually never called."

Which makes sense since I don't believe the ValidateMessage method is being mocked properly.

Please help I am going bald from ripping out my hair over this.

0
Accepted
Ricky
Telerik team
answered on 03 Jun 2011, 11:54 AM
Hi Chris,

Basically to avoid this kind of static method issues, you have to move the initialization to ClassInitalize not the setup itself. Here is one example that shows how you can initialize in this regard:

http://www.telerik.com/community/forums/justmock/general-discussions/mocks-failing-to-work-correctly-in-large-test-runs.aspx

Please let us know if you are still having problems.



Kind Regards
Mehfuz
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
0
Chris
Top achievements
Rank 1
answered on 03 Jun 2011, 01:26 PM
I saw that post and thought I was doing it like described (but then realized I wasn't) so that part is fixed. I have another issue I am fighting similar to this one dealing with private methods and partial mocking.

Basically it is the same situation where I have a test method that mocks a nonpublic member.  When that test method runs by itself the method is mocked correctly, however when run as part of a larger test run it fails.

I can't do the Mock.Partial<t>().For since it is a non public.  any suggestions?
0
Ricky
Telerik team
answered on 08 Jun 2011, 10:01 AM
Hi Chris,

Thanks again for sending the issue. Yes you right that Mock.Partial should have options for NonPublic members as well. I am logging this as a bug to be fixed in some of the upcoming builds



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
Chris
Top achievements
Rank 1
Answers by
Chris
Top achievements
Rank 1
Ricky
Telerik team
Share this question
or