I'm using moq with nunit and my test doesn't give me a fail or pass. It says it doesn't have a default constructor. I suspect I am not doing something correctly with injecting my interface into the constructor.
DonorManagementTests
[TestFixture]
public class DonorManagementTests
{
private readonly Mock<IValidation> _mockValidation;
private readonly DonorManagement _donorManagement;
public DonorManagementTests(IValidation validation)
{
_mockValidation = new Mock<IValidation>();
_donorManagement = new DonorManagement(_mockValidation.Object);
}
[Test, Description("View correct gift aid to two decimal places")]
public void DonorViewGiftAid()
{
const int donation = 20;
_mockValidation.Setup(x => x.ValidateDonation(donation)).Returns(20.00m);
var res = _donorManagement.GiftAidAmount(donation);
Assert.IsInstanceOf(typeof (decimal), res);
_mockValidation.Verify(x => x.ValidateDonation(donation), Times.Once);
}
}
DonorManagement
public class DonorManagement : IDonor
{
private readonly IValidation _validation;
public DonorManagement(IValidation validation)
{
_validation = validation;
}
public virtual decimal GiftAidAmount(decimal donationAmount)
{
const decimal gaRatio = 17.5m / (100 - 17.5m);
return _validation.ValidateDonation(donationAmount) * gaRatio;
}
}
Any ideas what I need to change in my code?
your test class must have a default C'tor.
change your test class to:
[TestFixture]
public class DonorManagementTests
{
private Mock<IValidation> _mockValidation;
private DonorManagement _donorManagement;
[SetUp]
public TestInit()
{
_mockValidation = new Mock<IValidation>();
_donorManagement = new DonorManagement(_mockValidation.Object);
}
[Test, Description("View correct gift aid to two decimal places")]
public void DonorViewGiftAid()
{
const int donation = 20;
_mockValidation.Setup(x => x.ValidateDonation(donation)).Returns(20.00m);
var res = _donorManagement.GiftAidAmount(donation);
Assert.IsInstanceOf(typeof (decimal), res);
_mockValidation.Verify(x => x.ValidateDonation(donation), Times.Once);
}
}
now each test will be isolated and you'll be able to execute your tests.
NUnit documentation (http://www.nunit.org/index.php?p=testFixture&r=2.5) says:
A non-parameterized fixture must have a default constructor.
A parameterized fixture must have a constructor that matches the parameters provided.
Related
Can someone show me how I can mock the result of a method in a base abstract class? See the very basic sample code below to demonstrate the problem and I need to mock the “GetAge()” method’s result. See the commented line at the end with the ending "<----- FIX here" where I think need to add the fix.
Customer Service
public interface ICustomerService
{
string GetCustomerDetailsSrv(int age);
}
public class CustomerService : ICustomerService
{
public string GetCustomerDetailsSrv(int age)
{
return $"Name: John Doe. Age: {age}";
}
}
Base Controller
public abstract class MyBaseController : ControllerBase
{
public virtual int GetAge()
{
return 7;
}
}
Customer Controller
public class CustomerController : MyBaseController
{
private readonly ICustomerService _customerSrv;
public CustomerController(ICustomerService customerSrv)
{
_customerSrv = customerSrv;
}
[HttpGet]
public string GetCustomerDetails()
{
var age = GetAge();
var result = _customerSrv.GetCustomerDetailsSrv(age);
return result;
}
}
Customer Controller Test
[TestClass]
public class CustomerControllerTest
{
private readonly CustomerController _controller;
private readonly Mock<ICustomerService> _mockSrv;
public CustomerControllerTest()
{
_mockSrv = new Mock<ICustomerService>();
_controller = new CustomerController(_mockSrv.Object);
}
[TestMethod]
public void TestGet()
{
//Arrange
int mockAge = 11;
string expectedResult = $"Name: Alice Smith. Age: {mockAge}";
// _controller.Setup(Controller => Controller.GetAge()).Returns(mockAge); <----- FIX here
_mockSrv.Setup(repo => repo.GetCustomerDetailsSrv(mockAge)).Returns(expectedResult);
//Act
var actualResult = _controller.GetCustomerDetails();
//Assert
Assert.IsTrue(actualResult == expectedResult);
}
}
I think the following code achieves what you want.
Creating a Mock from a CustomerController allows the setup the virtual method GetAge while still being able to use the GetCustomerDetails method from the CustomerController class.
[TestClass]
public class CustomerControllerTest
{
private readonly Mock<CustomerController> _mockController;
private readonly Mock<ICustomerService> _mockSrv;
public CustomerControllerTest()
{
_mockSrv = new Mock<ICustomerService>();
_mockController = new Mock<CustomerController>(() => new CustomerController(_mockSrv.Object));
}
[TestMethod]
public void TestGet()
{
//Arrange
int mockAge = 11;
string expectedResult = $"Name: Alice Smith. Age: {mockAge}";
_mockController.Setup(Controller => Controller.GetAge()).Returns(mockAge);
_mockSrv.Setup(repo => repo.GetCustomerDetailsSrv(mockAge)).Returns(expectedResult);
//Act
var actualResult = _mockController.Object.GetCustomerDetails();
//Assert
Assert.IsTrue(actualResult == expectedResult);
}
}
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);
Given
public interface IService
{
void Run<T>(T value, Func<T, string> func);
}
public class Sut
{
private readonly IService m_service;
public Sut(IService service)
{
m_service = service;
}
public void Test()
{
Point point = new Point {X = 1, Y = 2};
m_service.Run(point, p => $"X:{p.X}, Y:{p.Y}");
}
}
internal class Point
{
public int X { get; set; }
public int Y { get; set; }
}
And tested like
[Test]
public void RunTest()
{
// Arrange
Mock<IService> mockService = new Mock<IService>();
var sut = new Sut(mockService.Object);
// Act
sut.Test();
// Assert
mockService.Verify(e => e.Run(It.IsAny<It.IsAnyType>(), It.IsAny<Func<It.IsAnyType, string>>()), Times.Once);
}
This test will failed, due to Point is third party internal class, I can not use It.IsAny<Point> in the verify.
if I change the Point to the public and verify use following, it's working good.
mockService.Verify(e => e.Run(It.IsAny<Point>(), It.IsAny<Func<Point, string>>()), Times.Once);
any help would be appreciated
Generally, you want to put an InternalsVisibleTo attribute in the assembly under test, pointing to the test assembly.
See here for reference information.
I am trying mock internal method in the same class.But my mocking fails.
Here is my code.
Interface
public interface IStudentService
{
int GetRank(int studentId);
IList<Subject> GetSubjects(int studentId);
}
Implementation
public class StudentService : IStudentService
{
private readonly IStudentRepository _studentRepository;
private readonly ISubjectRepository _subjectRepository;
public StudentService(IStudentRepository studentRepository, ISubjectRepository subjectRepository)
{
_studentRepository = studentRepository;
_subjectRepository = subjectRepository;
}
public int GetRank(int studentId)
{
IList<Subject> subjects = GetSubjects(studentId);
int rank = 0;
//
//Calculate Rank
//
return rank;
}
public virtual IList<Subject> GetSubjects(int studentId)
{
return _subjectRepository.GetAll(studentId);
}
}
Unit Test
[TestFixture]
public class StudentServiceTest
{
[SetUp]
public void Setup()
{
}
[TearDown]
public void TearDown()
{
}
[Test]
public void GetRankTest()
{
using (var mock = AutoMock.GetStrict())
{
var mockStudentService = new Mock<IStudentService>();
mockStudentService.Setup(x => x.GetSubjects(1)).Returns(new ServiceResponse<SystemUser>(new List<Subject>{ new AccounProfile(), new AccounProfile()}));
mock.Provide(mockStudentService.Object);
var component = mock.Create<StudentService>();
int rank = component.GetRank(1);
mockStudentService.VerifyAll();
Assert.AreEqual(1, rank, "GetRank method fails");
}
}
}
When I debug the code it is not mocking the GetSubjects method. It actually go inside to that method. I am using Nunit, Moq and Autofac to write unit test.
Thanks In Advance!
There are two solutions.
1. Partial mocking
In this approach you create mock of component you're testing (StudentService) and tell Moq to mock some of its methods (GetSubjects -- to-be-mocked methods must be virtual), while delegating others (GetRank) to base implementation:
Setting mock.CallBase = true instructs Moq to delegate any call not matched by explicit Setup call to its base implementation.
// mockStudentService is not needed, we use partial mock
var service = mock.Create<StudentService>();
service.CallBase = true;
service.Setup(m => m.GetSubjects(1)).Returns(...);
var rank = service.GetRank(1);
// you don't need .VerifyAll call, you didn't not set any expectations on mock
Assert.AreEqual(1, rank, "GetRank method fails");
2. Mocking internal service (ISubjectRepository)
Partial mocks are reserved for special cases. Your case is rather common one. Instead of mocking itself, your component (StudentService) could rely on mocked ISubjectRepository to provide subjects for it:
using (var mock = AutoMock.GetStrict())
{
var subjectRepositoryMock = new Mock<ISubjectRepository>();
subjectRepositoryMock.Setup(x => x.GetSubjects(1)).Returns(...);
mock.Provide(subjectRepositoryMock.Object);
var component = mock.Create<StudentService>();
int rank = component.GetRank(1);
// verify is not needed once again
Assert.AreEqual(1, rank, "GetRank method fails");
}
This code works for. Thank you for everyone for support
[TestFixture]
public class StudentServiceTest
{
private Mock<StudentRepository> _studentRepositoryMock;
private Mock<SubjectRepository> _subjectRepositoryMock;
private Mock<StudentService> _studentServiceMock;
[SetUp]
public void Setup()
{
_studentRepositoryMock = new Mock<StudentService>(MockBehavior.Strict);
_subjectRepositoryMock = new Mock<SubjectRepository>(MockBehavior.Strict);
_studentServiceMock = new Mock<StudentService>(_studentRepositoryMock.Object, _subjectRepositoryMock.Object);
_studentServiceMock.CallBase = true;
}
[TearDown]
public void TearDown()
{
}
[Test]
public void GetRankTest()
{
_studentServiceMock.Setup(x => x.GetSubjects(1)).Returns(...);
int rank = component.GetRank(1);
_studentServiceMock.VerifyAll();
Assert.AreEqual(1, rank, "GetRank method fails");
}
}
I guess your GetSubjects method must be declared virtual, otherwise it cannot be mocked.
public virtual IList<Subject> GetSubjects(int studentId)
{
// code here
}
I'm new to this, so doubtlessly I'm doing something silly.
I am trying to create a mock object:
mockCurrencyConversion = mocks.NewMock<ICurrencyConversion>();
and getting this error:
The non-generic method 'NMock2.Mockery.NewMock(System.Type)' cannot be
used with type arguments
ICurrencyConversion:
public interface ICurrencyConversion
{
decimal convertCurrency(string fromCurrency, string toCurrency, decimal amount);
int addNumbers(int i, int j);
decimal getRate(CurrencyRateResponse rates, string fromCurrency);
CurrencyRateResponse getCurrencyRates();
HttpWebRequest GetWebRequest(string formattedUri);
}
My Test Code:
[TestFixture]
public class WhygoTest
{
private Mockery mocks;
private ICurrencyConversion mockCurrencyConversion;
[SetUp]
public void SetUp()
{
mocks = new Mockery();
mockCurrencyConversion = mocks.NewMock<ICurrencyConversion>();
}
[Test]
public void MyAddTest()
{
var cc = new CurrencyConversion();
Assert.AreEqual(cc.addNumbers(1, 2), 3);
}
}
Use the NewMock() method with the type as an argument:
mockCurrencyConversion =
(ICurrencyConversion) mocks.NewMock(typeof(ICurrencyConversion));