dimanche 26 juin 2016

unit testing explicit cast error on interface

I'm having an issue with a unit test failing due to an explicit cast error of an interface. I am not sure if there is a solution for this, but I haven't found one clear enough yet for my particular scenario.

In my constructor I pass in the generic interface of the data model. Then later on I explicitly cast that Idatamodel to my specific implementation. This fails to execute at run time in my unit test.

        public EventFinder(IEventFinder Successor,
                                  IAssetDataModel theAssetDataModel,
                                  IAssetEventHandler theEventHandler)
        : base(Successor, theAssetDataModel, theEventHandler)
    {
    }

//later on in the I call this cast to reference the model and method I need
                Listofstuff =  ((IMyOtherDataModel)_AssetDataModel).GetListofStuff();

Here is where the error fails in my unit test. I have a stub of this method, but it fails the explicit cast at run time and I am still a little confused as to why. I created a stub of this model in the unit test,but it still fails

            var stub_my_Data_model = new StubImyAssetDataModel();
        stub_my_Data_model.Getstuff = () =>
            {
                List<string> results = new List<string>();
                results.Add("test1");
                return results;
            };

Aucun commentaire:

Enregistrer un commentaire