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
0 votes
Upgraded from 4.1.0 to 4.2.3, and now, I cannot mock this static methods. The code below works fine in 4.1.0.

using (RecordExpectations recorder = RecorderManager.StartRecording())
           {
               AmazonS3Wrapper.Instance();
               recorder.Return(null);
}


public static AmazonS3Wrapper Instance()
        {
            if (singleton == null)
            {
                if (ConfigurationManager.AppSettings["MockingFileStorage"] != "true")
                {
                    singleton = new AmazonS3Wrapper();
                }
                else
                {
                    singleton = new MockAmazonS3Wrapper();
                }
            }
            return singleton;
        }



will give me

TypeMock.TypeMockException:
*** Cannot use Return in this sequence, there must be a mocked statement first
Perhaps you are trying to mock a method from mscorlib

Any ideas?
asked by M. Johansson (2.2k points)

3 Answers

0 votes
Hi,

This should be working. :?
In fact I've tried reproducing it over here with no success (the test works fine).

Can you by any chance post the entire declaration of AmazonS3Wrapper class? (just the members declaration without actual code inside will do).

If we cant find anything that might suggest why this is not working we will need to take it offline and try investigating the logs.
answered by lior (13.2k points)
0 votes
I just put together the following code based on your snippets and it passes fine for me under NUnit 2.4.6 and Typemock Isolator 4.2.3:

public class AmazonS3Wrapper
{
  public static AmazonS3Wrapper Instance()
  {
    throw new Exception("We should never get here - we're mocking.");
  }
}

[TestFixture]
[VerifyMocks]
public class MyTestFixture
{
  [Test]
  public void MyTest()
  {
    using(RecordExpectations recorder = RecorderManager.StartRecording())
    {
      AmazonS3Wrapper.Instance();
      recorder.Return(null);
    }
    AmazonS3Wrapper wrapper = AmazonS3Wrapper.Instance();
    Assert.IsNull(wrapper);
  }
}


You'll notice I truncated the contents of the Instance method. Since you're mocking it, the contents never get executed so we know the problem isn't in there - we can remove it so it doesn't confuse the issue. By throwing an exception, we know that if we get the exception then mocking setup isn't happening right and that's where we should start looking.

What I'm suspecting is happening is you're seeing an error for something else - some code you didn't post. Generally when I run into these problems it's because I've got something else in the test or in the test setup that is mocking something it shouldn't be mocking (or is setting up mocking incorrectly).

Check out the rest of your test code and test setup code. If it's still not working, I recommend creating a very small reproduction - that is, a whole separate project with a whole separate test fixture and a minimal copy of the code you're working with. (Something small like what I posted). See what the smallest amount of code is that is required to get the error.

If you still can't figure it out, post your small repro and we can work it out from there.
answered by tillig (6.7k points)
0 votes
Heh. Looks like I need to be quicker on the draw! :)
answered by tillig (6.7k points)
...