locked
unit testing against an interface. RRS feed

  • Question

  • User-963208184 posted

    HI all,

    I had a set of unit tests that was working originally because they were written against a specific class i.e.

    protected List<SWReportItem> _results;
    
            protected override void Because_of()
            {
                base.Because_of();
                _results = _service.GetSWNewJoiners(1);
            }
    
    [TestMethod]
            public void then_the_pre_rdr_charge_Basis_is_populated()
            {
                _results.ForEach(c => c.PreRDRChargeBasis.ShouldEqual("PreRDRChargeBasis"));
            }

    This worked fine as _service.GetSWNewJoiners(1) returned a list<SWReportItem>

    As the code has been re-factored this method now returns a list of interfaces.

    And ultimately gets populated with :

    List<ICurrentSelectionProviderReportItem> result = new List<ICurrentSelectionProviderReportItem>();
                SWReportItem reportItem = null;
                foreach (BasePensionReportItem item in newJoinerSelections)
                {
                    reportItem = new SWReportItem(item);
                    PopulateItem(reportItem);
                    result.Add(reportItem);
                }
                return result;

    So as you can see ultimately the interface has been populated with a typeof(SWReportItem)

    normally I might do something like:

    var castObject = _result.Where(c => c.GetType() == typeof(SWReportItem)).Cast<SWReportItem>().ToList());
    
    
    

    However in this scenario I haven't got the Where clause available to me, as I would have then performed the test against my variable.

    I thought then that I could do something like:

     _results.ForEach(c => c.GetType() == typeof(SWReportItem).GetMember("PreRDRChargeBasis").ShouldEqual("PreRDRChargeBasis"));

    but it still complains. How should I re-write my test

    Wednesday, December 3, 2014 7:16 AM

Answers

  • User-963208184 posted
    [TestMethod]
            public void then_the_pre_rdr_charge_Basis_is_populated()
            {
                foreach (SWReportItem reportItem in _results)
                {
                    reportItem.PreRDRChargeBasis.ShouldEqual("PreRDRChargeBasis");
                }
                
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 3, 2014 7:26 AM