jeudi 24 décembre 2015

Unit Testing configurable Mock object

I just learning Test double and I have a problem with implementation of configurable mock object (the expected outputs can be set at runtime and the verification can be done by he mock object itself).

My test code is below. I need to implement configurable mock object without using mock object framework as like as below.

 [TestMethod]

       public void SaveOrderAndVerifyExpectations()
        {

     IShopDataAccess dataAccess = mocks.CreateMock<IShopDataAccess>();
     Order o = new Order(6, dataAccess);
     o.Lines.Add(1234, 1);
     o.Lines.Add(4321, 3);
     // Record expectations
     dataAccess.Save(6, o);
     // Start replay of recorded expectations
     mocks.ReplayAll();

     o.Save();
     mocks.VerifyAll();
 }

Other class where take a place above code.

    public interface IShopDataAccess
    {
        decimal GetProductPrice(int productId);

        void Save(int orderId, Order o);
    }
}


And this is an example about this topic but I don't understand what does it do?

 internal class MockShopDataAccess : IShopDataAccess
    {
        private ImplementationCallback implement_;

        internal MockShopDataAccess(ImplementationCallback callback)
        {
            this.implement_ = callback;
        }

        #region IShopDataAccess Members

        public decimal GetProductPrice(int productId)
        {
            MemberData member = new MemberData("GetProductPrice");
            member.Parameters.Add(new ParameterData("productId", productId));    
            this.implement_(member);

            return (decimal)member.ReturnValue;
        }

        public void Save(int orderId, Order o)
        {
            MemberData member = new MemberData("Save");
            member.Parameters.Add(new ParameterData("orderId", orderId));
            member.Parameters.Add(new ParameterData("o", o));

            this.implement_(member);
        }

        #endregion
    }



As a result, I need to implement configurable mock object at the first code and I dont know what do I need to do? I suppose , I should write a new class ? Can someone lead by example , thanks for any response.

Aucun commentaire:

Enregistrer un commentaire