1

I am new to mocking and i just started to use moq. The tool states that we can use moq for mocking classes without interfaces. I was wondering if it is posible when mocking to keep the internal state of the class. For example:

//define class for mocking
var mock = new Mock<ILoveThisFramework>();

//define
mock.Setup(framework => framework.DownloadExists("2.0.0.0"))
      .Returns(true)
      .AtMostOnce();

//use mocking tools to call methods and return defined results

The question: althought the results were defined, have the internal state (e.g. private class variables) changed? (keep the class state?)

zeus2
  • 309
  • 2
  • 11

2 Answers2

1

I assume you want to verify private or internal state of an object - your unit under test.

In unit testing, you shouldn't care about that. Private or internal state is NOT something you should test. You could, but you shouldn't. Usually public API and public state are covered by the unit tests.

There are a few good answers on this topic already, I 'd refer you there for more options:

Community
  • 1
  • 1
oleksii
  • 35,458
  • 16
  • 93
  • 163
  • Take a look at this one also: http://stackoverflow.com/questions/249847/how-do-you-test-private-methods-with-nunit – tobbenb3 Apr 08 '14 at 11:33
  • @oleksii i want to verify that calling a method via moq the class variables that this method modifies are changed. – zeus2 Apr 08 '14 at 11:38
  • Are you looking for something like [Arrange Act Assert](https://www.google.co.uk/search?q=Arrange+Act+Assert+moq) and specifically for the Assert stage? – oleksii Apr 08 '14 at 11:42
1

I assume you are asking if calling DownloadExists on the mock changes the class state of the mocked ILoveThisFramework instance.

The answer to that is no. Your base method is not called, just the mocked version created by .Setup().

There is a long blog post here detailing a way to add in a way to call the base method from your mock, which is probably what you want to do.

However, since that object is mocked, you normally wouldn't care about its other properties and if they are correctly set, because it is not the object under test. If the object under test does care about other properties on ILoveThisFramework then those properties should just also be mocked to return the expected results, and not rely on the internals of that class working correctly.

CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
  • in my project i need to you the mocking in a different way. So i am trying to find a way to call a method, change the variables, while mocking an internal call to another method. Thank you – zeus2 Apr 08 '14 at 12:32