locked
Writing test using Mock RRS feed

  • Question

  • User-1000104858 posted

    Hi!

    I try to write a test to verify that my function works as expected but i cant get it to work. I use LazyCache(https://www.nuget.org/packages/LazyCache.AspNetCore/2.0.0-beta03) to cache data in my asp.net core 2.1 website. I have this:

        [TestClass]
        public class MainTest
        {
            [TestMethod]
            public void Test1()
            {
                    var s = new DataHelper(new Mock<ICacheHelper>().Object);
                    var data = s.GetDashboard("demo");
                    Assert.IsTrue(data.Name == "demo");
            }
        }
        public interface IDataHelper
        {
            Dashboard GetDashboard(string name);
        }
    
        public class DataHelper : IDataHelper
        {
            private readonly ICacheHelper _cache;
    
            public DataHelper(ICacheHelper cache)
            {
                _cache = cache;
            }
    
            public Dashboard GetDashboard(string name)
            {
                    Dashboard dbFunc() => GetDashboardData(name);
                    return _cache.GetOrAddDashboard(name, dbFunc);
            }
    	private Dashboard GetDashboardData(string name) {
    		return new Dashboard { Name = "demo" };
    	}
        }
        public interface ICacheHelper
        {
            Dashboard GetOrAddDashboard(string name, Func<Dashboard> dbFunc);
        }
    
        public class CacheHelper : ICacheHelper
        {
            private readonly IAppCache _cache;
    
            public CacheHelper(IAppCache cache)
            {
                _cache = cache;
            }
    
            public Dashboard GetOrAddDashboard(string name, Func<Dashboard> dbFunc)
            {
                return _cache.GetOrAdd("dashboard:" + name, dbFunc);
            }
       }

    When i run this, data.Name = null. I set breakpoints where i marked the code with blue color but they never hits. When i run my website, there is no problem but how can i write a test to verify this function?

    Thursday, December 13, 2018 9:27 PM

Answers

  • User1120430333 posted

    You are not testing anything all you have done is set-up a mock that is incomplete. You have not set up what you are trying to mock. You are not doing the Actual/Act that executes code the that uses the mocked object.  That's why you see the Return(mocked data) from a mocked object's method, if you were doing the mock properly.

    https://docs.telerik.com/devtools/justmock/basic-usage/arrange-act-assert

    No matter what unit testing and mocking framework you use, you must follow the basic principles of unit testing and mocking if mocking is needed.

    Arrange  setup what is need to run the test, like give mocked test data that the mocked method in a mocked  object should return, set up the mocked object and method being  mocked so that it returns the mocked data. 

    Act -- Actually run a test using real code,  and if the actual code being tested is supposed to execute the mocked object's method  to return mocked  data, then the mocked object would return the mocked data from the mocked object's method during real/actual code execution. 

    Assert- verify that the test acted as expected, which would be the mocked data that returned from the Actual/Act execution of real code.

    Actually, you should make the unit test fail, and then you make the unit test pass, which is called the red, green and reactor, which are already doing the red as your test has failed.  :)

    https://dzone.com/articles/pattern-of-the-month-red-green-refactor

            [TestMethod]
            public void Test1()
            {
                    var s = new DataHelper(new Mock<ICacheHelper>().Object);
                    var data = s.GetDashboard("demo");
                    Assert.IsTrue(data.Name == "demo");
            }
        }
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 14, 2018 7:45 AM