how to mock static external call

2 posts, 0 answers
  1. Mohd
    Mohd avatar
    13 posts
    Member since:
    Jan 2013

    Posted 13 Feb 2013 Link to this post

    Help me to complete the test methods for below code
    I have one method OrderProducts in Shop class  which  place the order or request new stock when products are not there.
    Can you help me mock the local call and external call.
    There is method OrderProducts and against each line of the code I provided the comments what I want to mock in the Testmethods. 

    Copy paste the below code in one .cs file of JustMockTest project
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.ObjectModel;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
      
     
    namespace Business
    {
        public class Shop
        {
            public Shop() { }
            public void OrderProducts()
            {
                LogInfo log = new LogInfo();//From the test method should be able to  skip  this constructor assume there is big amount of the dependent code in the constructor   
                CategoryType cat = new CategoryType();
                cat.Level = 2; //From the test method should be able to set cat.Level equal to 10
                var categories = CategoryHelper.GetCategories(cat); //want pass cat level 3 and return any three item
                this.CategoriesList = categories;
                bool retval = true; //From the test method should be able to set retval equal to false
                IProductService productProxy = WCFServicesInstance.GetWCFServiceInstance<IProductService>();
                 
                List<Product> productlist = productProxy.ProductsByCategories(log, CategoriesList);
                
               if(productlist.Count>0)
               {
                    
                   int orderID=PlacetheOrder(productlist); //From the test method should be able to call another method like DoInstead
     
               }
               else 
               {
                   RequestNewStock(productlist);//From the test method should be able to call another method like DoInstead
               }
     
                 OnClose(); //From the test method should be able to ignore the call
            }
     
            private int PlacetheOrder(List<Product> productlist)
            {
                throw new NotImplementedException();
            }
     
            private void RequestNewStock(List<Product> productlist)
            {
                throw new NotImplementedException();
            }
     
            private void OnClose()
            {
                 throw new NotImplementedException();
            }
            private  List<Category> categoriesList;
     
            public  List<Category> CategoriesList
            {
                get { return categoriesList;}
                set { categoriesList = value;}
            }
         
     
     
     
        }
     
        public class Product
        {
        }
     
        public class LogInfo
        {
        }
        public class Category
        {
           private string name;
     
        public string Name
        {
            get { return name;}
            set { name = value;}
        }
         
        }
        public class CategoryType
        {
            private int level;
     
            public int Level
            {
                get { return level; }
                set { level = value; }
            }
             
        }
     
        public class CategoryHelper
        {
           public static List<Category> GetCategories(CategoryType cat)
            {
                throw new NotImplementedException();//This making external call
            }
        }
     
        public interface IProductService : IDisposable
        {
          
         List<Product> ProductsByCategories(LogInfo log,List<Category> CategoriesList);
        }
        public class WCFServicesInstance
        {
     
            public WCFServicesInstance() { }
            public static T GetWCFServiceInstance<T>() where T : class, IDisposable
            {
                T retVal;
                           
                retVal = GetProxyInstance<T>().ProxyContract;
                return retVal;
            }
            public static WCFServiceProxy<T> GetProxyInstance<T>() where T : class
            {
                 
                WCFServiceProxy<T> wcfProxy = null;
                #region There is external call that gives the instance
                //External dlll that gives instance of WCFServiceProxy<T>
                #endregion
                return wcfProxy;
            }
        }
        public class WCFServiceProxy<T> : ClientBase<T> where T : class
        {
            public WCFServiceProxy() {}
            public T ProxyContract { get; set; }
        }
    }
     
    Here are the tried test method.  You can paste it another .cs file

    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Business;
    using Telerik.JustMock;
    namespace JustMockTestProject
    {
        [TestClass]
        public class ShopTest
        {
            [TestMethod]
            public void OrderProductsPlaceOrderTest()
            {
                #region Arrange
                var loginfo = Mock.Create<v>();
                CategoryType cat = new CategoryType();
                cat.Level = 3;
               //  Mock.Arrange(() => CategoryHelper.GetCategories(cat)).Returns(cat);
                #endregion
                #region Act
                Shop target = new Shop();
                target.OrderProducts();
                #endregion
                #region Assert
                //Assert that Order is place i.e PlacetheOrder is called(mock version)
     
                #endregion
            }
     
            [TestMethod]
            public void OrderProductsRequestNewStockTest()
            {
                #region Arrange
                #endregion
                #region Act
                Shop target = new Shop();
                target.OrderProducts();
                #endregion
                #region Assert
                //Assert that Product are Requested for the new stock i.e RequestNewStock is called (mock version)
                #endregion
            }
        }
    }


  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 15 Feb 2013 Link to this post

    Replied in ticket with ID: 658940

    Greetings,
    Kaloyan
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
Back to Top