how to stub a property out using rhino for unit testing - c#

Someone shoot me as I can't see what I'm missing but the inner property of my stubbed object is null which is breaking my tests.
Many Thanks,
James
CommandService
public void Create()
{
var risk = _queryService.GetRisk(creatable.HeaderId);
if(risk.HeaderId != null) // HeaderId IS NULL??
{
...
}
}
QueryService
public Risk GetRisk(int headerId)
{
return _repository.GetRisk(headerId);
}
Unit test
[TestInitialize]
public void SetUp()
{
_mockRepository = new MockRepository();
_queryService = _mockRepository.Stub<IQueryService>();
_commandService = new CoreCommandService(_queryService);
}
public void MyTest()
{
var runRisk = new RunRisk { HeaderId = 10 };
_queryService.Stub(x => x.GetRisk(199)).Repeat.Any().Return(runRisk);
var result = _commandService.Create();
}

need to use the following to fix it.
MockRepository.GenerateMock<IQueryService>();

Related

XUnit before all (fixture) doesn't work for me

I'm trying to fill the In-Memory database with 3 data objects. I'm using a fixture for that purpose.
My Fixture :
public class UBSeedDataFixture : IDisposable
{
public AzDbContext DBContext { get; private set; }
public UBSeedDataFixture()
{
var options = new DbContextOptionsBuilder<AzDbContext>()
.UseInMemoryDatabase(databaseName: "InMemoryTestDb")
.Options;
DBContext = new AzDbContext(options);
DBContext.UBs.Add(new UB { Title = "UB1", Code = "SomeCode" });
DBContext.UBs.Add(new UB { Title = "UB2", Code = "SomeCode" });
DBContext.UBs.Add(new UB { Title = "UB3", Code = "SomeCode" });
DBContext.SaveChanges();
}
public void Dispose()
{
DBContext.Database.EnsureDeleted();
DBContext.Dispose();
}
}
then I run two simple tests:
public class UBTests : IClassFixture<UBSeedDataFixture>
{
UBSeedDataFixture fixture;
public UBTests(UBSeedDataFixture fixture)
{
this.fixture = fixture;
}
[Fact]
public void DeleteTest()
{
var result = fixture.DBContext.UBs.SingleOrDefault(u => u.Title == "UB1");
fixture.DBContext.Remove(result);
fixture.DBContext.SaveChanges();
result = fixture.DBContext.UBs.SingleOrDefault(u => u.Title == "UB1");
Assert.Equal(null, result);
}
[Fact]
public void AddTest()
{
var ubs = fixture.DBContext.UBs.ToList();
Assert.Equal("UB1", ubs[0].Title);
}
}
As the result, I get an error that I get more than one result when I query data with Title == "UB1". If I debug my tests I have 6 elements in my database, when my fixture is supposed to create only 3. It seems like my fixture runs every time when a new test runs. How can I fix it so I have a fixture something like "Once before all tests"?

How to do the Assert phase of a private method whose return type is Void?

I have a class with a private method
public class MyClass
{
private void SomeMethod(PrimaryAllocationDP packet)
{
........................
some code
........................
packet.AllocatedAgency = AgencyAllocated;
}
}
Now by using MSUnit Testing framework, I have written so far
[TestMethod]
public void TestAllocatedAgency()
{
var packet = new Fixture().Create<PrimaryAllocationDP>(); //used AutoFixture here
PrivateObject accessor = new PrivateObject(new MyClass());
accessor.Invoke("SomeMethod", packet); //Act
// what will be the Assert? Since it is void
}
What will be the Assert? Since it is void, how can I write the assert?
Well given that in the example the method under test is making a change to its argument/dependency you could assert that the desired result of calling the function is that the packet's AllocatedAgency property is in fact not null
[TestMethod]
public void TestAllocatedAgency() {
//Arrange
var packet = new Fixture().Create<PrimaryAllocationDP>(); //used AutoFixture here
var sut = new MyClass();
var accessor = new PrivateObject(sut);
//Act
accessor.Invoke("SomeMethod", packet);
//Assert
Assert.IsNotNull(packet.AllocatedAgency);
}
If it is possible for you to change PrimaryAllocationDP you can also add a new interface IPrimaryAllocationDP and test the property setting. In my test I am assuming AllocatedAgency is of type object and I am using Moq. But maybe you can also use AutoFixture for mocking? To make it more clear I set AgencyAllocated directly in MyClass
[TestFixture]
public class DependencyInjection
{
[TestMethod]
public void TestAllocatedAgency()
{
var packet = new Mock<IPrimaryAllocationDP>();
PrivateObject accessor = new PrivateObject(new MyClass());
accessor.Invoke("SomeMethod", packet.Object); //Act
packet.VerifySet(p => p.AllocatedAgency = 42);
}
}
public interface IPrimaryAllocationDP
{
//object or any other type
object AllocatedAgency { set; }
}
public class PrimaryAllocationDP : IPrimaryAllocationDP
{
public object AllocatedAgency { set; private get; }
}
public class MyClass
{
private readonly object AgencyAllocated = 42;
private void SomeMethod(IPrimaryAllocationDP packet)
{
//........................
//some code
//........................
packet.AllocatedAgency = AgencyAllocated;
}
}

Mock Setup Exception for valid setup

I have the following classes structure:
public class MyObj
{
public int Number;
}
public interface IService
{
int ProcessInt(MyObj obj);
}
public class Service : IService
{
public int ProcessInt(MyObj myObj)
{
return myObj.Number;
}
}
and then the consumer class
public class Class1
{
public void Run(IService s)
{
var obj = new MyObj {Number = 1};
Console.WriteLine(s.ProcessInt(obj));
}
}
and then the unit test
[TestFixture]
public class MyTest
{
private readonly Mock<IService> _service = new Mock<IService>(MockBehavior.Strict);
private readonly Class1 _sut = new Class1();
[SetUp]
public void SetUp()
{
var obj = new MyObj {Number = 1};
_service.Setup(x => x.ProcessInt(obj)).Returns(1);
}
[Test]
public void TestClass1()
{
_sut.Run(_service.Object);
}
}
The problem I have is that when I run the unit test, I get "Moq.MockException : IService.ProcessInt(MoqStuff.MyObj) invocation failed with mock behavior Strict.All invocations on the mock must have a corresponding setup." which is weird since I have the setup for that input.
Is this an expected behavior of Moq framework? Is there any way I can fix that?
Thank you
You have this code in your SetUp method:
var obj = new MyObj {Number = 1};
_service.Setup(x => x.ProcessInt(obj)).Returns(1);
Here you set up expectation, that ProcessInt is called with this specific object obj.
However, in your method Run you create another object:
var obj = new MyObj {Number = 1};
Though property value is the same, this object is totally different from the one you created in your SetUp method. This is the reason why you get exception about missing setup.
What you can do instead is set up your service for any input parameter using It.IsAny:
[SetUp]
public void SetUp()
{
_service.Setup(x => x.ProcessInt(It.IsAny<MyObj>())).Returns(1);
}
This setup will work for any parameter value.
Or if you want to match only based on some criteria, you can use It.Is:
[SetUp]
public void SetUp()
{
// setup only for cases where obj.Number == 1
_service
.Setup(x => x.ProcessInt(It.Is<MyObj>(o => o.Number == 1 )))
.Returns(1);
// setup only for cases where obj.Number == 2
_service
.Setup(x => x.ProcessInt(It.Is<MyObj>(o => o.Number == 2 )))
.Returns(2);
}

Rhino Mocks help needed

enter code hereI have following method where i need to write unit test case to test this method.
public void ProcessDormantApplications()
{
List<long> dormantApplicationIDs = new List<long>();
dormantApplicationIDs = service.GetDormantApplications();
if (dormantApplicationIDs.Count > 0)
{
foreach (long dormantID in dormantApplicationIDs)
{
string msg = service.UpdateDormantApplications(dormantID);
}
}
}
}
and this is the TEST method i wrote.
[TestClass]
public class DormantApplicationsTest
{
ILogger logger;
IService Service;
[TestInitialize]
public void SetUp()
{
logger = MockRepository.GenerateStub<ILogger>();
Service = MockRepository.GenerateStub<IService>();
}
[TestMethod]
public void TESTProcessDormantApplications()
{
////arrange
////act
var target = new BLogic(service, logger);
target.ProcessDormantApplications();
////assert
// service.AssertWasCalled(x => x.
}
}
The actual method calls another service layer which inturn invokes web service to get data. In this scenario I am not sure what to ASSERT in this situation.
[TestMethod]
public void CheckProcessDormantApplications_InBetweenApplicationFailedToUpdate()
{
////arrange
var applicationIds = new List<long>()
{
1,2,3
};
UpdateResponse.isSuccess = true;
UpdateResponse.errorMessage = string.Empty;
Service.Stub(x => x.GetDormantApplications()).Return(applicationIds);
for(int i=0; i <= applicationIds.Count-1; i++)
{
if (i == 1) //set this application id response to FALSE so it should continnue with next record as well
{
UpdateResponse.isSuccess = false;
UpdateResponse.errorMessage = "making it fail for test";
}
Service.Stub(x =>x.UpdateDormantApplications(applicationIds[i])).Return(UpdateResponse);
}
////act
var target = new BLogic(Service, logger);
target.ProcessDormantApplications();
////assert
foreach (long id in applicationIds)
{
Service.AssertWasCalled(x => x.UpdateDormantApplications(id));
}
}
}
Based on the code you provide you have to set a behavior on GetDormantApplications which return some ids, then verify that UpdateDormantApplications was called for each id:
[TestMethod]
public void Check_ProcessDormantApplications()
{
////arrange
var applicationId = new List<long>()
{
//put here some ids
};
DormantServiceAdapter.Stub(x => x.GetDormantApplications()).Return(applicationId);
var target = new DormantApplicationsBusinessLogic(DormantServiceAdapter, logger);
////act
target.ProcessDormantApplications();
////assert
foreach (var id in applicationId)
{
DormantServiceAdapter.AssertWasCalled(x => x.UpdateDormantApplications(id));
}
}

How to return null when accessing a moq object?

I am using Moq library for unit testing. Now what i want is that when I access my object for the first time it should return null, and when i access this on second time it should return something else.
here is my code
var mock = new Mock<IMyClass>();
mock.Setup(?????);
mock.Setup(?????);
var actual = target.Method(mock.object);
in my method i am first checking that whether mock object is null or not, if it is null then do initialize it and then do some calls on it.
bool Method(IMyClass myObj)
{
if (myObj != null)
return true;
else
{
myObj = new MyClass();
bool result = myObj.SomeFunctionReturningBool();
return result;
}
}
what to do setup for mock object,
Also i need to know how to mock this line
bool result = myObj.SomeFunctionReturningBool();
It sounds like you are trying to run two tests with one test method - maybe it would be better to split the tests into two?
You also want to initialise a new object if the method is passed null. To test this, I suggest creating a factory object responsible for creating instances of MyClass. The new code would look like:
interface IMyClassFactory
{
IMyClass CreateMyClass();
}
bool Method(IMyClass myObj, IMyClassFactory myClassFactory)
{
if (myObj != null)
{
return true;
}
myObj = myClassFactory.CreateMyClass();
return myObj.SomeFunctionReturningBool();
}
Then the tests would look like:
[Test]
public void Method_ShouldReturnTrueIfNotPassedNull()
{
Assert.That(target.Method(new MyClass()), Is.True);
}
[Test]
public void Method_ShouldCreateObjectAndReturnResultOfSomeFunctionIfPassedNull()
{
// Arrange
bool expectedResult = false;
var mockMyClass = new Mock<IMyClass>();
mockMyClass.Setup(x => x.SomeFunctionReturningBool()).Returns(expectedResult);
var mockMyFactory = new Mock<IMyClassFactory>();
mockMyFactory.Setup(x => x.CreateMyClass()).Returns(mockMyClass.Object);
// Act
var result = target.Method(null, mockMyFactory.Object);
// Assert
mockMyClass.Verify(x => x.SomeFunctionReturningBool(), Times.Once());
mockMyFactory.Verify(x => x.CreateMyClass(), Times.Once());
Assert.That(result, Is.EqualTo(expectedResult));
}
Here the factory pattern has been used to pass in an object which can create objects of IMyClass type, and then the factory itself has been mocked.
If you do not want to change your method's signature, then create the factory in the class's constructor, and make it accessible via a public property of the class. It can then be overwritten in the test by the mock factory. This is called dependency injection.
Moq - Return null - This working example simply illustrates how to return null using Moq. While the line of code is required is the commented line below, a full working example is provided below.
// _mockShopService.Setup(x => x.GetProduct(It.IsAny<string>())).Returns(() => null);
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
}
public interface IShopService
{
Product GetProduct(string productId);
}
public class ShopService : IShopService
{
public Product GetProduct(string productId)
{
if (string.IsNullOrWhiteSpace(productId))
{
return new Product();
}
return new Product { Id = "8160807887984", Name = "How to return null in Moq" };
}
}
public class Shop
{
private static IShopService _shopService;
public Shop(IShopService shopService)
{
_shopService = shopService;
}
public Product GetProduct(string productId)
{
Product product = _shopService.GetProduct(productId);
return product;
}
}
[TestClass]
public class ShopTests
{
Mock<IShopService> _mockShopService;
[TestInitialize]
public void Setup()
{
_mockShopService = new Mock<IShopService>();
}
[TestMethod]
public void ShopService_GetProduct_Returns_null()
{
//Arrange
Shop shop = new Shop(_mockShopService.Object);
//This is how we return null --- all other code above is to bring this line of code home
_mockShopService.Setup(x => x.GetProduct(It.IsAny<string>())).Returns(() => null);
//Act
var actual = shop.GetProduct(It.IsAny<string>());
//Assert
Assert.IsNull(actual);
}
}
To mock a result value you can do simply:
mock.Setup(foo => foo.SomeFunctionReturningBool()).Returns(true); // or false :)
for the other question, just pass null in the unit test instead of passing mock.object and your unit test cover that too. So you basically create two unit test one with:
var actual = target.Method(mock.object);
and the other one with:
var actual = target.Method(null);
Currently your SUT is tight-coupled with MyClass implementation. You can't mock objects which are instantiated with new keyword inside your SUT. Thus you cannot test your SUT in isolation, and your test is not unit test anymore. When implementation of MyClass.SomeFunctionReturningBool will change (it will return true instead of false), tests of your SUT will fail. This shouldn't happen. Thus, delegate creation to some dependency (factory) and inject that dependency to your SUT:
[Test]
public void ShouldReturnTrueWhenMyClassIsNotNull()
{
Mock<IMyClassFactory> factory = new Mock<IMyClassFactory>();
Mock<IMyClass> myClass = new Mock<IMyClass>();
var foo = new Foo(factory.Object);
Assert.True(foo.Method(myClass.Object));
}
[Test]
public void ShouldCreateNewMyClassAndReturnSomeFunctionValue()
{
bool expected = true;
Mock<IMyClass> myClass = new Mock<IMyClass>();
myClass.Setup(mc => mc.SomeFunctionReturningBool()).Returns(expected);
Mock<IMyClassFactory> factory = new Mock<IMyClassFactory>();
factory.Setup(f => f.CreateMyClass()).Returns(myClass.Object);
var foo = new Foo(factory.Object);
Assert.That(foo.Method(null), Is.EqualTo(expected));
factory.VerifyAll();
myClass.VerifyAll();
}
BTW assignment new value to method parameter does not affect reference which you passed to method.
Implementation:
public class Foo
{
private IMyClassFactory _factory;
public Foo(IMyClassFactory factory)
{
_factory = factory;
}
public bool Method(IMyClass myObj)
{
if (myObj != null)
return true;
return _factory.CreateMyClass().SomeFunctionReturningBool();
}
}
You can use TestFixture with parameter. this test will run two times and different type value.
using NUnit.Framework;
namespace Project.Tests
{
[TestFixture(1)]
[TestFixture(2)]
public class MyTest
{
private int _intType;
public MyTest(int type)
{
_intType = type;
}
[SetUp]
public void Setup()
{
if (_intType==1)
{
//Mock Return false
}
else
{
//Mock Return Value
}
}
}
}

Categories

Resources