Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
56 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 16 additions & 20 deletions
36
UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/prod/ProfileDao.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace UnitTestWithStaticFunctions.prod | ||
{ | ||
public static class ProfileDao | ||
{ | ||
private static IProfileDao _profileDao; | ||
|
||
////just simulate data source | ||
//private static Dictionary<string, string> fakePasswordDataset = new Dictionary<string, string> | ||
//{ | ||
// {"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"); | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
UnitTestWithStaticFunctions/UnitTestWithStaticFunctions/prod/ProfileDaoImpl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, string> fakePasswordDataset = new Dictionary<string, string> | ||
//{ | ||
// {"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(); | ||
} | ||
} | ||
} |