1. Install nuget Packages - Moq
2. Code Setup :-
Business Object :
public class QuestionnaireServiceModel {
public string QuestionnaireTitle { get; set; }
public IList<string> QuestionsText { get; set; }
}
Interface
public interface IQuestionnaireService
{
Task<QuestionnaireServiceModel> GetAllQuestionnaire();
}
Service
public class QuestionnaireService : IQuestionnaireService
{
private readonly IQuestionnaireQueryService questionnaireQueryService;
public QuestionnaireService(IQuestionnaireQueryService _questionnaireQueryService)
{
questionnaireQueryService = _questionnaireQueryService;
}
public async Task<QuestionnaireServiceModel> GetAllQuestionnaire()
{
return await questionnaireQueryService.GetAllQuestionnaire();
}
}
3. Test Case for the above Service
[TestFixture]
public class QuestionnaireServiceTests
{
private IQuestionnaireService _questionnaireService;
private Mock<IQuestionnaireQueryService> _questionnaireQueryService;
[SetUp]
public void SetUp()
{
_questionnaireQueryService = new Mock<IQuestionnaireQueryService>();
_questionnaireQueryService.Setup(qs => qs.GetAllQuestionnaire())
.Returns(Task.FromResult(new QuestionnaireServiceModel() {
QuestionnaireTitle = "Test",
QuestionsText = It.IsAny<List<string>>()
}));
_questionnaireService = new QuestionnaireService(_questionnaireQueryService.Object);
}
[Test]
public async Task ShouldGetAllQuestionnaire()
{
//Arrange
var expectedTitle = "Test";
//Act
var result =await _questionnaireService.GetAllQuestionnaire();
//Assert
Assert.That(result.QuestionnaireTitle, Is.EqualTo(expectedTitle));
}
}
4. Controller
public class QuestionnaireController : Controller
{
private readonly IConfiguration _config;
private readonly IQuestionnaireService _questionnaireService;
private readonly IMapper _mapper;
public QuestionnaireController(IConfiguration config, IQuestionnaireService questionnaireService, IMapper mapper)
{
_config = config;
_questionnaireService = questionnaireService;
_mapper = mapper;
}
public ViewResult Index(QuestionnaireViewModel qModel=null)
{
if (qModel!=null && !string.IsNullOrEmpty(qModel.QuestionnaireTitle))
{
return View(qModel);
}
else
{
QuestionnaireServiceModel listQuestionnaire = _questionnaireService.GetAllQuestionnaire().Result;
var questionnaireViewModel = _mapper.Map<QuestionnaireViewModel>(listQuestionnaire);
return View(questionnaireViewModel);
}
}
}
5. Unit Test of the above controller
[TestFixture]
public class QuestionnaireControllerTests
{
private Mock<IConfiguration> _config;
private QuestionnaireController _questionnaireController;
[SetUp]
public void SetUp()
{
_config = new Mock<IConfiguration>();
Mock<IQuestionnaireService> _questionnaireService = new Mock<IQuestionnaireService>();
_questionnaireService.Setup(qs => qs.GetAllQuestionnaire())
.Returns(Task.FromResult(new QuestionnaireServiceModel()
{ QuestionnaireTitle = It.IsAny<string>(),
QuestionsText = It.IsAny<List<string>>()
}));
var mockMapper = new Mock<IMapper>();
_questionnaireController = new QuestionnaireController(_config.Object, _questionnaireService.Object, mockMapper.Object);
}
[Test]
public void ShouldGetQuestions()
{
//Arrange
var expectedTitle = "My expected quesitons";
var mockModel = Mock.Of<QuestionnaireViewModel>(x => x.QuestionnaireTitle == "My expected quesitons");//Add Mock
//Act
var result = (QuestionnaireViewModel)_questionnaireController.Index(mockModel).ViewData.Model;
//Assert
Assert.That(result.QuestionnaireTitle, Is.EqualTo(expectedTitle));
}
}
No comments:
Post a Comment