Skip to content

Commit

Permalink
1. add internal setter for unit test to inject stub object
Browse files Browse the repository at this point in the history
2. add a ProfileDaoImpl class and extract the original static function content to this class
  • Loading branch information
hatelove committed Jun 12, 2015
1 parent 3ef9fa2 commit 87195b9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
Expand Up @@ -63,6 +63,7 @@
<Compile Include="AuthServiceTest.cs" />
<Compile Include="prod\IProfileDao.cs" />
<Compile Include="prod\ProfileDao.cs" />
<Compile Include="prod\ProfileDaoImpl.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
@@ -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");
}
}
}
}
@@ -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();
}
}
}

0 comments on commit 87195b9

Please sign in to comment.