I'm starting to build up Unit Tests for a project we have. We've decided upon Moq to assist with the 'Mocking' of the repositorys as we dont want to run the tests against the live DB.
I'm obviously using Moq incorrectly, How would one write the GetMessage Test? The first 2 seem to work fine.
The return value of the GetMessage Test is allways null so the test fails
private Mock<IMessageRepository> _mockRepository;
private IMessageBoardService _service;
[TestInitialize]
public void Initialize()
{
_mockRepository = new Mock<IMessageRepository>();
_service = new MessageBoardService(_mockRepository.Object);
}
[TestMethod]
public void CreateMessage()
{
var result = _service.CreateMessage("Test", "Description", 8000, 0);
Assert.IsNotNull(result);
}
[TestMethod]
public void CreateComment()
{
var Message = _service.CreateMessage("Test", "Description", 8000, 0);
var Result = _service.CreateComment("Test Comment", Message.MessageID, 0);
Assert.IsNotNull(Result);
}
[TestMethod]
public void GetMessage()
{
var Message = _service.CreateMessage("Test", "Description", 8000, 0);
_service.AddMessage(Message);
_service.Save();
var RetMessage = _service.GetMessage(Message.MessageID); //Always returns Null
Assert.IsNotNull(RetMessage);
}
EDIT= == ===============================
What about the following?
[TestMethod]
public void GetMessage()
{
var tmpMessage = _service.CreateMessage("Test", "Description", 5, 0);
_mockRepository.Setup(r => r.GetMessage(It.IsAny<int>()))
.Returns(tmpMessage);
var RetMessage = _service.GetMessage(tmpMessage.MessageID);
Assert.IsNotNull(RetMessage);
}