From 87195b9b96a02981a61bbcfe167a943b003161e7 Mon Sep 17 00:00:00 2001 From: Joey Chen Date: Fri, 12 Jun 2015 18:53:28 +0800 Subject: [PATCH] 1. add internal setter for unit test to inject stub object 2. add a ProfileDaoImpl class and extract the original static function content to this class --- .../UnitTestWithStaticFunctions.csproj | 1 + .../prod/ProfileDao.cs | 36 ++++++++--------- .../prod/ProfileDaoImpl.cs | 39 +++++++++++++++++++ 3 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/prod/ProfileDaoImpl.cs diff --git a/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions.csproj b/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions.csproj index 99cb842..0894cf3 100644 --- a/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions.csproj +++ b/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions.csproj @@ -63,6 +63,7 @@ + diff --git a/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/prod/ProfileDao.cs b/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/prod/ProfileDao.cs index dcd6a54..4cd7c76 100644 --- a/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/prod/ProfileDao.cs +++ b/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/prod/ProfileDao.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; namespace UnitTestWithStaticFunctions.prod { @@ -9,35 +8,32 @@ public static class ProfileDao { private static IProfileDao _profileDao; - ////just simulate data source - //private static Dictionary fakePasswordDataset = new Dictionary - //{ - // {"joey","1234"}, - // {"demo","!@#$"}, - //}; + //add internal setter for unit test to inject stub object + internal static IProfileDao MyProfileDao + { + get + { + if (_profileDao == null) + { + // add a ProfileDaoImpl class and extract the original static function content to this class + _profileDao = new ProfileDaoImpl(); + } + + return _profileDao; + } + set { _profileDao = value; } + } internal static string GetPassword(string account) { // 轉接到 interface 的方法 return _profileDao.GetPassword(account); - - //if (!fakePasswordDataset.ContainsKey(account)) - //{ - // throw new Exception("account not exist"); - //} - - //return fakePasswordDataset[account]; } internal static string GetToken(string account) { // 轉接到 interface 的方法 return _profileDao.GetToken(account); - - ////just for demo, it's insecure - //var seed = new Random((account.GetHashCode() + (int)DateTime.Now.Ticks) & 0x0000FFFF); - //var result = seed.Next(0, 999999); - //return result.ToString("000000"); } } -} +} \ No newline at end of file diff --git a/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/prod/ProfileDaoImpl.cs b/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/prod/ProfileDaoImpl.cs new file mode 100644 index 0000000..61b725f --- /dev/null +++ b/UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/prod/ProfileDaoImpl.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UnitTestWithStaticFunctions.prod +{ + public class ProfileDaoImpl : IProfileDao + { + ////just simulate data source + //private static Dictionary fakePasswordDataset = new Dictionary + //{ + // {"joey","1234"}, + // {"demo","!@#$"}, + //}; + + public string GetPassword(string account) + { + //if (!fakePasswordDataset.ContainsKey(account)) + //{ + // throw new Exception("account not exist"); + //} + + //return fakePasswordDataset[account]; + + throw new NotImplementedException(); + } + + public string GetToken(string account) + { + ////just for demo, it's insecure + //var seed = new Random((account.GetHashCode() + (int)DateTime.Now.Ticks) & 0x0000FFFF); + //var result = seed.Next(0, 999999); + //return result.ToString("000000"); + + throw new NotImplementedException(); + } + } +}