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
Hi...

I am trying to assign a value to a ref parameter of a method and it doesn't seem to work. The parameter is of type DateTime in C#.

My method:
private bool IsNewFileAvailable(string file, ref DateTime localFile)
{
...
}

I need to modify the 'DateTime localFile'.

I am doing so by:
myMock.ExpectAndReturn("IsNewFileAvailable", true).Args(Check.IsAny(), new Assign(new DateTime(2005, 1, 1)));

but the the next call to IsNewFileAvailable will not modify it's reference to localFile, but keeps it's initial value (which in my case is 'DateTime myLocalFile = new DateTime();')

I am not sure what is going on because it works well with type of bool, string, List<string>, etc... but obviously not with DateTime... why?

- rick -
asked by rickbear (640 points)

3 Answers

0 votes
Hi,

I tried reproducing what you describe here, and it works ok with DateTime. Here's the test I'm using:
        [TestMethod]
        public void TestMethod1()
        {
            Mock<SUT> myMock = MockManager.Mock<SUT>();
            DateTime ret = new DateTime(2005, 1, 1);
            myMock.ExpectAndReturn("IsNewFileAvailable", true).Args(Check.IsAny(), new Assign(ret));

            SUT mySUT = new SUT();
            DateTime now = DateTime.Now;
            Assert.IsTrue(mySUT.IsNewFileAvailable("", ref now));
            Assert.AreEqual(ret, now);
            MockManager.Verify();
        }



Note that in this case the IsNewFileAvailable method is public, but it also works for private method.
If this does not help you, we'll take it offline. Let me know.
answered by gilz (14.5k points)
0 votes
This is also, what I am doing. I contruct the mock in a different way though:

Mock myMock = MockManager.Mock(typeof(myFTPUtil), Constructor.NotMocked);

But I think I found my problem now. On the developer machine I use an older version of TypeMock (I do not have access right now to see which version exactly, but I remember it is old because it has to match another buildserver's version of TypeMock). And on my own machine the 'ref DateTime localFile' changes without problems (Version 4.2.3.0).
answered by rickbear (640 points)
0 votes
Hi,

I'm glad you've found the solution.
Let me need further assistance.
answered by gilz (14.5k points)
...