vendredi 9 septembre 2016

c# unit test fails with moq when it goes to hit a real service

So I was put on doing unit test and i noticed different unit tests failing in nunit and in Visual Studio with Resharper i tried debugging it and I get object

   [Test]
    public void KeyDocumentService_ProofKeyDocument_RepoReturnsData_ServiceReturnsTheDataWithoutError()
    {
        //Arrange
        KeyDocumentProofRequest request = new KeyDocumentProofRequest() { KeyDocumentId = 2 };
        string returnedResponse = "2";
        KeyDocument keyDocumentResponse = new KeyDocument() { CampaignId = "2", DesignFileId = 3,DocumentId="2", DataSourceId="3", KeyDocumentId=1 };
        List<vwKeyDocumentSearch> keyListResponse = new List<vwKeyDocumentSearch>() { new vwKeyDocumentSearch { FieldName = "test", FieldValue = "testvalue" } };
        var uproduceRepo = new Mock<IUProduceRepository>();
        var keyDocRepo = new Mock<IKeyDocumentRepository>();
        var templateRepo = new Mock<ITemplateRepository>();
        keyDocRepo.Setup(p => p.GetKeyDocument(It.IsAny<KeyDocumentRequest>())).Returns(new KeyDocumentResponse() { data = keyDocumentResponse });
        keyDocRepo.Setup(p => p.GetKeyDocumentItems(It.IsAny<int>())).Returns(keyListResponse);
        uproduceRepo.Setup(p => p.ProduceDocument(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Customization[]>(), It.IsAny<string>(), It.IsAny<string>(), null)).Returns(returnedResponse);
        // Act.
        KeyDocumentService svc = new KeyDocumentService(keyDocRepo.Object, uproduceRepo.Object, templateRepo.Object);
        var response = svc.ProofKeyDocument(request);

        //Assert
        Assert.IsNotNull(response);
        Assert.IsNotNull(response.data.JobId);
        Assert.IsNull(response.Error);
    }

So the error is happening on this line :

var response = svc.ProofKeyDocument(request);

Are unit Tests supposed to even be going into a real service? or is that ok?

That Method ProofKeyDocument looks like this FYI

    private List<Customization> GetCustomizationsFromKeyDocumentItems(List<vwKeyDocumentSearch> keyDocumentItemsList,
                                        int templateId, int clientId)
    {
        try
        {
            List<Customization> KeyDocumentCustomizations = new List<Customization>();

            var keyDocumentVariableList = keyDocumentItemsList.Where(k => k.Type.ToUpper()=="VARIABLE").ToList();
            var keyDocumentSettingList = keyDocumentItemsList.Where(k => k.Type.ToUpper() == "SETTING").ToList();
            var keyDocumentContentList = keyDocumentItemsList.Where(k => k.Type.ToUpper() == "CONTENT").ToList();

            KeyDocumentCustomizations.AddRange(VariableCustomizations(keyDocumentVariableList, templateId));
            KeyDocumentCustomizations.AddRange(SettingCustomizations(keyDocumentSettingList, templateId));
            KeyDocumentCustomizations.AddRange(ContentCustomizations(keyDocumentContentList, templateId, clientId));

            return KeyDocumentCustomizations;
        }
        catch (Exception ex)
        {
            logger.Error(string.Format("Error customizing key document: {0}", templateId), ex);
            throw ex;
        }
    }

I see with Debugging it blowing up on this line var keyDocumentVariableList = keyDocumentItemsList.Where(k => k.Type.ToUpper()=="VARIABLE").ToList();

Object Reference not set to instance... error Why ?

Aucun commentaire:

Enregistrer un commentaire