Rhino mocks stub vs expect, always choosing first one why? - c#

I went with this solution:
Stubbing a property twice with rhino mocks
but even when I change both of my Stubs to .Expect, the first Expect is winning out:
Here's the recreation in mono:
using System;
using NUnit.Framework;
using Rhino.Mocks;
namespace FirstMonoClassLibrary
{
[TestFixture]
public class TestingRhinoMocks
{
Sut _systemUnderTest;
IFoo _dependency;
[SetUp]
public void Setup()
{
_dependency = MockRepository.GenerateMock<IFoo>();
_dependency.Expect(x => x.GetValue()).Return(1);
_systemUnderTest = new Sut(_dependency);
}
[Test]
public void Test()
{
_dependency.Stub(x => x.GetValue()).Return(2);
var value = _systemUnderTest.GetValueFromDependency();
Assert.AreEqual(2, value); // Fails says it's 1
}
}
public interface IFoo
{
int GetValue();
}
public class Sut
{
private readonly IFoo _foo;
public Sut(IFoo foo)
{
_foo = foo;
}
public int GetValueFromDependency()
{
return _foo.GetValue();
}
}
}

you need to do the following:
[Test]
public void Test()
{
_dependency.BackToRecord();
_dependency.Expect(_ => _.GetValue).Return(2);
_dependency.Replay();
var value = _systemUnderTest.GetValueFromDependency();
value.ShouldBe(2); // Fails says it's 1
}

Related

Mock a method of class under test with Moq & AutoMock

I've been reading around and I can't seem to find a case that matches the behavior I want with Moq.
I want to mock a specific method (which is interfacing with an external API) of a class that I want to test. The problem is that, as I understand it, once the class is .Create<T>, you cannot .Setup()... any method on it.
Here is an example:
public class ClassA
{
public void ConfigureExpansion()
{
var classB = new ClassB();
var data = GetExternalData(); // I want to mock this
//data is used in the code
classB.TaskB(data); // I want to mock this
}
public string GetExternalData()
{
return data...
}
}
public class ClassB
{
public void TaskB(String myData)
{
//Does some work
}
}
[TestMethod]
public void Test()
{
using (var mock = AutoMock.GetLoose())
{
mock.Mock<IClassB>().Setup(x => x.TaskB(It.IsAny<string>()));
var cls = mock.Create<ClassA>();
cls.Setup(x => x.GetExternalData(It.IsAny<string>())).Return("MyData"); // <--- This is not valid, but represents what I am trying to do.
//act
cls.ConfigureExpansion();
mock.Mock<IClassB>().Verify(x => x.TaskB(), Times.Once);
}
}
I tried to set up a mock for the interface of my class under test, but the cls won't call it:
[TestMethod]
public void Test()
{
using (var mock = AutoMock.GetLoose())
{
mock.Mock<IClassA>().Setup(x => x.GetExternalData(It.IsAny<string>())).Return("MyData"); //<--- Mocking the method here is not detected by the cls below
mock.Mock<IClassB>().Setup(x => x.TaskB(It.IsAny<string>()));
var cls = mock.Create<ClassA>();
//act
cls.ConfigureExpansion();
mock.Mock<IClassB>().Verify(x => x.TaskB(), Times.Once);
}
}
I've also tried instantiating my class-under-test and when I do it like this, it doesn't take the mocks (ClassB) into consideration:
[TestMethod]
public void Test()
{
using (var mock = AutoMock.GetLoose())
{
mock.Mock<IClassA>().Setup(x => x.GetExternalData(It.IsAny<string>())).Return("MyData"); //<--- Mocking the method here is not detected by the cls below
mock.Mock<IClassB>().Setup(x => x.TaskB(It.IsAny<string>()));
var cls = new ClassA();
//act
cls.ConfigureExpansion();
mock.Mock<IClassB>().Verify(x => x.TaskB(), Times.Once);
}
}
I would really appreciate any insights about this,
Thank you
Here is one approach for your problem. If you want to test ConfigureExpansion of ClassA, you have to mock the dependencies used in that method, which is ClassB. You cannot mock GetExternalData of ClassA because ClassA is the class under test now. You may have to refactor your classes in a way so that there is a ClassUnderTest which creates both instances of ClassA and ClassB using dependency injection. Then, you can use Moq to mock the interfaces IClassA and IClassB to test the ConfigureExpansion.
public class ClassUnderTest
{
private readonly IClassA _classA;
private readonly IClassB _classB;
public ClassUnderTest(IClassA classA, IClassB classB)
{
this._classA = classA;
this._classB = classB;
}
public void ConfigureExpansion()
{
var data = this._classA.GetExternalData();
this._classB.TaskB(data);
}
}
public interface IClassA
{
string GetExternalData();
}
public class ClassA : IClassA
{
public string GetExternalData()
{
return "some data";
}
}
public interface IClassB
{
void TaskB(string myData);
}
public class ClassB : IClassB
{
public void TaskB(string myData)
{
//Does some work
}
}
The test method can be:
[TestMethod]
public void Test()
{
using (var mock = AutoMock.GetLoose())
{
var mockClassA = mock.Mock<IClassA>();
var mockClassB = mock.Mock<IClassB>();
mockClassA.Setup(x => x.GetExternalData()).Returns("MyData");
var cls = new ClassUnderTest(mockClassA.Object, mockClassB.Object);
//act
cls.ConfigureExpansion();
mockClassB.Verify(x => x.TaskB(It.IsAny<string>()), Times.Once);
}
}

Partial mock methods in a class using Autofixture, Moq and XUnit

I want to mock only some methods of a class and call the real implementation for other methods.
I have my sut class Test where the Runner class is injected in the constructor. This injected class has again a injected other class RunnerParam in the constructor.
The code is a simplified case of my real classes in trying to have only the basics.
[Fact]
public void Test()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var paramMock = fixture.Freeze<Mock<IRunnerParam>>();
paramMock.Setup(x => x.Multiplicator()).Returns(2);
var classMock = fixture.Freeze<Mock<IRunner>>();
classMock.Setup(x => x.Run()).Returns(5);
var test = fixture.Create<Test>();
var result = test.StartRunning(); // should be 5
var result2 = test.StartRunningImplementation(5); // should be 500
}
Supporting members
public interface IRunnerParam
{
int Multiplicator();
}
public class RunnerParam : IRunnerParam
{
public virtual int Multiplicator()
{
return 20;
}
}
public interface IRunner
{
int Run();
int RunImplementation(int param);
}
public class Runner : IRunner
{
protected virtual RunnerParam MultiParam { get; set; }
public Runner(RunnerParam multiParam)
{
MultiParam = multiParam;
}
public virtual int Run()
{
return 10;
}
public int RunImplementation(int param)
{
return 10 * MultiParam.Multiplicator() * param * Run();
}
}
public class Test
{
private readonly IRunner _runner;
public Test(IRunner runner)
{
_runner = runner;
}
public int StartRunning()
{
return _runner.Run();
}
public int StartRunningImplementation(int param)
{
return _runner.RunImplementation(param);
}
}
I want to mock and give a mocked value to the method Run in the class Runner, but to use the real implementation of the method RunImplementation.
I would expect to see for result2 500, but it's 0, meaning that the method is not seen as mocked up. In my eyes that is correct, but the Moq callbase is equal to true, so the real implementation should be taken, but it isn't.
What am I missing here?
In the shown simplified example, Test is only dependent on IRunner
private readonly IRunner _runner;
public Test(IRunner runner)
{
_runner = runner;
}
So that is all that needs to be mocked if the intention was to test Test class in isolation.
//...
var classMock = fixture.Freeze<Mock<IRunner>>();
classMock.Setup(x => x.Run()).Returns(5);
classMock.Setup(x => x.RunImplementation(It.IsAny<int>())).Returns(500);
//...
If Runner class is to be also tested in isolation, then a mocked RunnerParam would be needed to satisfy its dependencies.
It should however be dependent on the abstraction (interface) and not the concretion (implementation).
protected virtual IRunnerParam MultiParam { get; set; }
public Runner(IRunnerParam multiParam) {
MultiParam = multiParam;
}
This simplifies the isolated test as described in the original question
I want to mock and give a mocked value to the method Run in the class Runner, but to use the real implementation of the method RunImplementation.
//Arrange
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var runnerParam = fixture.Freeze<Mock<IRunnerParam>>()
.Setup(_ => _.Multiplicator())
.Returns(2);
var subjectMock = fixture.Freeze<Mock<Runner>>();
subjectMock.CallBase = true;
subjectMock.Setup(_ => _.Run()).Returns(5);
int expected = 500;
Runner sut = subjectMock.Object;
//Act
var actual = sut.RunImplementation(5); // should be 500
//Assert
actual.Should().Be(expected);

Assert and Verify both fail

Please see the code below:
[TestClass]
public class UnitTest1
{
Moq.Mock<ISayGoodMorning> GoodMorningMock;
[TestInitialize]
public void init()
{
GoodMorningMock = new Moq.Mock<ISayGoodMorning>();
GoodMorningMock.Setup(x => x.GoodMorning()).Returns(Test());
}
[TestMethod]
public void TestMethod1()
{
GoodMorningMock.Verify(x => x.GoodMorning(), Times.Once());
Assert.AreEqual(g.SayGreeting(), "Hola");
}
public string Test()
{
return "Hola";
}
}
The assert and the verify fail. Why is that? Here is the code that is tested:
namespace ClassLibrary1
{
public class SayGoodMorning : ISayGoodMorning
{
public string GoodMorning()
{
return "Good Morning";
}
}
public interface ISayGoodMorning
{
string GoodMorning();
}
}
namespace ClassLibrary2
{
public class Greeting
{
public string SayGreeting()
{
ISayGoodMorning s = new SayGoodMorning();
return s.GoodMorning();
}
}
}
First the subject under test needs to be refactored to inject the dependency to allow a mock to be used when testing. Either via constructor or method injection. (Explicit Dependencies Principle)
This example uses constructor injection
namespace ClassLibrary2 {
public class Greeting {
private readonly ISayGoodMorning speaker;
public Greeting(ISayGoodMorning speaker) {
this.speaker = speaker;
}
public string SayGreeting() {
return speaker.GoodMorning();
}
}
}
Next the test needs to be restructured so that the Verify is done after exercising the method under test;
[TestClass]
public class UnitTest1 {
Mock<ISayGoodMorning> GoodMorningMock;
[TestInitialize]
public void init() {
//Arrange
GoodMorningMock = new Mock<ISayGoodMorning>();
GoodMorningMock.Setup(_ => _.GoodMorning()).Returns(Test());
}
[TestMethod]
public void TestMethod1() {
//Arrange cont'd
var g = new ClassLibrary2.Greeting(GoodMorningMock.Object);
var expected = "Hola";
//Act
var actual = g.SayGreeting();
//Assert
GoodMorningMock.Verify(_ => _.GoodMorning(), Times.Once());
Assert.AreEqual(expected, actual);
}
public string Test() {
return "Hola";
}
}

How to unit test constructor when using nested test classes

Assuming I have some logic in a class constructor that I wish to unit test, how would I structure it if I am using xUnit.net and nested test classes for each method?
using Xunit;
public class MyClass
{
public MyClass()
{
// Some logic to test
}
public int GetResult()
{
return 123;
}
}
public class MyClassFacts
{
// How to test MyClass constructor?
// Nested classes to test each of MyClass' methods
public class GetResult
{
[Fact]
public void IsCorrect()
{
var myClass = new MyClass();
int result = myClass.GetResult();
Assert.Equal(123, result);
}
// Other tests for MyClass.GetResult()...
}
}
You're already testing it in your IsCorrect() test technically but if you want an explicit test:
using Xunit;
public class MyClass
{
public MyClass()
{
// Some logic to test
}
public int GetResult()
{
return 123;
}
}
public class MyClassFacts
{
[Fact]
public void Ctor()
{
var myClass = new MyClass();
Assert.NotNull(myClass);
// Any other asserts you require
}
// Nested classes to test each of MyClass' methods
public class GetResult
{
[Fact]
public void IsCorrect()
{
var myClass = new MyClass();
int result = myClass.GetResult();
Assert.Equal(123, result);
}
// Other tests for MyClass.GetResult()...
}
}

Rhino mock AAA ExpectationViolationException

Getting Error while running rhinomock test method : Test method TestProject1.UnitTest2.TestMethod1 threw exception:
Rhino.Mocks.Exceptions.ExpectationViolationException: ITestInterface.Method1(5); Expected #1, Actual #0.
My code looks like:-
namespace ClassLibrary1
{
public interface ITestInterface
{
bool Method1(int x);
int Method(int a);
}
internal class TestClass : ITestInterface
{
public bool Method1(int x)
{
return true;
}
public int Method(int a)
{
return a;
}
}
}
And my test looks something like:-
using ClassLibrary1;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
namespace TestProject1
{
[TestClass]
public class UnitTest2
{
[TestMethod]
public void TestMethod1()
{
ITestInterface mockProxy = MockRepository.GenerateMock<ITestInterface>();
TestClass tc = new TestClass();
bool result = tc.Method1(5);
Assert.IsTrue(result);
mockProxy.AssertWasCalled(x => x.Method1(5));
}
}
}
Any help appreciated.
You expect ITestInterface.Method1 to be called, but it never does.
You don't use your mockProxy at all in the test code - you just create it and you create your own instance, but there's no relation between the two of them.
Your TestClass is not dependant upon any interface that you want to mock, similar example that does use the mock would be:
internal class TestClass
{
private ITestInterface testInterface;
public TestClass(ITestInterface testInterface)
{
this.testInterface = testInterface;
}
public bool Method1(int x)
{
testInterface.Method1(x);
return true;
}
}
[TestClass]
public class UnitTest2
{
[TestMethod]
public void TestMethod1()
{
ITestInterface mockProxy = MockRepository.GenerateMock<ITestInterface>();
TestClass tc = new TestClass(mockProxy);
bool result = tc.Method1(5);
Assert.IsTrue(result);
mockProxy.AssertWasCalled(x => x.Method1(5));
}
}
I think you should read more about using Rhino Mocks, e.g. Rhino Mocks AAA Quick Start?

Categories