ååãããããããã«ãMicrosoft Researchã®è£œå
-Microsoft Moles Isolation Frameworkã«ã€ããŠèª¬æããŸãã haberraiser
alek_sysã® æçš¿ãèªãã åŸãåããŠåœŒã«äŒããŸããã ç§ã¯ããšãŠã奜ãã ã£ãã®ã§ããã®äœ¿çšã®çµéšãå
±æããããšã«ããŸããã
ãªãã§ïŒ
æåã«ãMicrosoft.Molesãæå³ããç®çãšãMicrosoft.Molesã§äœãéæã§ããããå€æããŸãã
- ãã¹ããããããžãã¯ãå€éšç°å¢ããå®å
šã«åé¢ããŸãã
- ãŠãŒã¶ãŒããã¹ã察象ã¯ã©ã¹ã§ããã¯ã©ã¹ã®å®è£
ããªãå Žåã§ãããŠããããã¹ããè¿
éãã€ç°¡åã«äœæããã¯ã©ã¹ã®ããžãã¯ããã¹ãããæ©èœãå¯èœã«ãªããŸãã
- ãã¹ãããŒã¿ã»ãããæŽçããããé¢é£ãªããžã§ã¯ãã®ç¶æ
ãã·ãã¥ã¬ãŒãããŠãã¹ãæ¡ä»¶ãäœæãããããããšã容æã«ãªããŸãã
- æã«ã¯ãåäœãã¹ãã®å®è¡æéãççž®ããããã¹ãã®é »ç¹ãªèµ·åãçŸå®ã®ãã®ã«ãªããŸã
- ãŠãããããžãã¯ã®éåã¯ããã¹ãçšã«èšèšãããŠããªãæ°çŸãŸãã¯2ã€ã®ãã¹ãã®äœäžã䌎ããŸãã
- è€éãªã¯ãŒã¯ãããŒã䜿çšãã䟿å©ãªã¡ãœãããã¹ã
äžè¬çãªåäœãã¹ãã«ã€ããŠ
äžèšã®ç®æšã«çåãããå Žåã¯ãåäœãã¹ãã®äž»èŠãªãã€ã³ããæãåºããŠå°ãæŽæ°ãã䟡å€ããããŸãã çãã®ãªã人-次ã®
ã»ã¯ã·ã§ã³ã«ã¹ãããã§ããŸãã
ãŠããããã¹ããšã¯äœã§ããïŒ
- åã
ã®ããã°ã©ã ãŠããããåé¢ãããŠãããïŒãŸãã¯è€æ°ã®ãŠãããã®ã»ããïŒãåå¥ã«åäœããŠããããšã瀺ããŸãã
- ãŠãããã®ããžãã¯ãææžåãã-ãŠããããã©ã®ããã«æ©èœããããç解ããããã«-ãã¹ãã®ã³ãŒããèªãã ãã§ãã
- ãªãã¡ã¯ã¿ãªã³ã°ã®ç°¡çŽ å-å€æŽåŸãã³ãŒãããšã©ãŒãªãã§æ©èœããããšãç°¡åã«ç¢ºèªã§ããŸãã
- éçºããã»ã¹ã®ã»ãšãã©ã®ãšã©ãŒã®ä¿®æ£ã«ãããã°ä¿®æ£æéã®ççž®ã ååž°ã®å¯èœæ§ãæå°éã«ããŸãã
åäœãã¹ãã®èŠä»¶
- ãã¹ãã§ã¯ã¯ã©ã¹ã³ãŒãã®æå°éããã¹ãããå¿
èŠããããç¹å®ã®ã³ãŒãã®ãã¹ãæ°ã¯ã³ãŒãå®è¡ãã¹ã®æ°ã«æ¯äŸããå¿
èŠããããŸãã
- ãã¹ãã®å®è£
ã¯å¯èœãªéãåçŽã§ããã¹ãå€ã®ãã¹ãç°å¢ã®åæåãå¿
èŠãšããªããã®ã§ãªããã°ãªããŸããïŒããšãã°ãããŒã¿ããŒã¹å
ã®ãã¹ãããŒã¿ã®æºåïŒã
- ãã¹ãã¯èªå·±ææžåããå¿
èŠããããŸããå€éšã®éçºè
ã¯ããã®ãŠãããã«å®è£
ããããã¹ããèªãã§ããŠãããã®ããžãã¯ãç解ããå¿
èŠããããŸãã ããŒã¿ããŒã¹ã®å
容ããã¹ãããŒã¿ã®ä»ã®ãœãŒã¹ã調ã¹ãå¿
èŠãªã
- ãã¹ãã¯å¯èœãªéãã·ã³ãã«ã«ããå¿
èŠããããŸããããããã¹ãŠã®ãã¹ãã±ãŒã¹ããã¹ããã1ã€ã®ãã¹ãããããåã
ã®ãã¹ãã±ãŒã¹ã«å®è£
ããã12åã®ã·ã³ãã«ãªãã¹ããç解ããæ¹ãã¯ããã«ç°¡åã§ãã
- ãã¹ãã®èœäžã¯ããã¹ã察象å€ã®ããžãã¯ã«é¢é£ä»ããã¹ãã§ã¯ãããŸããã
- ãããã®ã·ã¹ãã ãšã®çžäºäœçšããã¹ãããããã«èšèšãããŠããªãå Žåããã¹ãã¯å€éšã·ã¹ãã ïŒããŒã¿ããŒã¹ãµãŒããŒãWebãµãŒããŒããã¡ã€ã«ã·ã¹ãã ãå€éšããã€ã¹ã³ã³ãããŒã©ãŒïŒã«äŸåããŠã¯ãªããŸããã
- æå°éã®å®è¡æéã§ãã¹ãã§ããæ倧ã®ã³ãŒãã«ãã¬ããž
åäœãã¹ãã䜿çšãããšãã«çããåé¡
- ãã¹ãç°å¢ã®æºåã®é£ãã-倧èŠæš¡ãªãã¹ãããŒã¿ã»ããã®äœæããµãŒããŒãžã®ããŸããŸãªæ¥ç¶ã®äœæãæ§æãã¡ã€ã«ã®èª¬æãå€éšããã€ã¹ã®ãšãã¥ã¬ãŒã¿ãŒãäœæããå¿
èŠæ§ãªã©ã
- ã¯ã©ã¹ã¯ä»ã®ã¯ã©ã¹ã®ãŠãŒã¶ãŒã§ããããšãå€ããããç¹å®ã®ã¯ã©ã¹ã®ãã¹ãã®è€éããããã³äœæ¥ã®ãšã©ãŒã®ååšã¯ãã¹ãã®äœäžã䌎ããŸããããã¯åäœãã¹ãã®ãã©ãã€ã ã«å¯Ÿå¿ããŸããã
- ãã¹ãã§ã³ãŒãã100ïŒ
網çŸ
ããããšã®é£ããïŒååãšããŠãããã¯éçºè
ã®æ ãšæåã®2ã€ã®åé¡ã«ãããã®ã§ãïŒ
- æåã®2ã€ã®çç±ã«èµ·å ãããã¹ãã®ç¶æïŒãããã°ãä¿®æ£ïŒã®è€éãã
- ã¯ã©ã¹ã®é衚瀺ã¡ã³ããŒã«ã¢ã¯ã»ã¹ã§ããªãïŒãªãã¬ã¯ã·ã§ã³ãé€ãïŒããã¹ãå€ã§èªã¿åãå°çšã¡ã³ããŒãåæåã§ããªãã
- å€ãã®å Žåããã¹ãã®å€±æã¯ãå¥ã®ãŠãããïŒãã¹ãæžã¿ã®ãŠãããã§ã¯ãªãïŒã®ããžãã¯ãéåããããšããäºå®ã«ãããã®ã§ãã ééã£ãå Žæã§ãšã©ãŒãæ¢ãã®ã«æéãè²»ããå¿
èŠããããŸãã
- ãã¹ãã®å®è¡æé-ãŠããããã¹ãã®äž»ãªèŠä»¶ã¯é »ç¹ã«å®è¡ããããšã§ãïŒãŠããããå€æŽãããã¹ããå®è¡ããŸããïŒãããã¹ãã®å®è¡ã«æéããããããããããå€ãã®å Žåãããã¯äžå¯èœã§ãã ãã¹ãã«çŽæ¥é¢ä¿ããªãããã»ã¹ã«è²»ããããã»ãšãã©ã®æé-ããŒã¿ããŒã¹ããã¡ã€ã«ã·ã¹ãã ãWebãµãŒããŒãªã©ã®èªã¿åã/æžã蟌ã¿ããã¹ãã«çŽæ¥é¢ä¿ããªãé¢é£ããžãã¯ã®åæåã
åäœãã¹ãããŒã«ãããã
çŸæç¹ã§ã¯ãããŸããŸãªç®çïŒãŠããããã¹ããçµ±åãã¹ããæ©èœãã¹ããUIãã¹ãïŒã®èªåãã¹ããäœæããã³äœ¿çšããããã«èšèšãããå€ãã®ããã°ã©ãã³ã°èšèªçšã®ãã¬ãŒã ã¯ãŒã¯ïŒNUnitãJUnitãDUnitãxUnitãMSTestïŒãå€æ°ãããŸãã ååãšããŠããã¹ããããã³ãŒããåšå²ã®ããžãã¯ããå®å
šã«åé¢ãããŠããããšãä¿èšŒã§ããªããããçŽç²ãªåœ¢åŒã§å®å
šãªåäœãã¹ããäœæããããšã¯ã§ããŸããã éçºè
ã¯ãããŸããŸãªããªãã¯ã«é Œããè¿œå ã®ã¢ãã¯ã¯ã©ã¹ã®ã¯ã©ãŠããäœæãããªãã¬ã¯ã·ã§ã³ã䜿çšããå¿
èŠããããŸãã ãããŠãããã§Isolation Frameworksã圹ç«ã¡ãŸããããã«ããã䟿å©ãã€è¿
éã«ãåäœãã¹ãçšã®åé¢ãããç°å¢ãæŽçã§ããŸããMicrosoftMolesããã®1ã€ã§ãã
Microsoft.Molesã¯äœãã§ããŸããïŒ
ã¢ã«ãç§ãã¡ã«äŸãæäŸããå¯èœæ§ãå®èšŒããããšããŸãïŒå¯èœãªéãåçŽã«ããããšããŸãããããã®äžã§ã¢ã«ã䜿çšããããšãæ£åœåããã®ã«ååãªã»ã©é£ããããŸããïŒã
ããŒã«ã«ãã¡ã€ã«ããã·ã¥ãšãªã¢ãŒããã¡ã€ã«ããã·ã¥ãäžèŽããªãå Žåã«ããªã¢ãŒãã¹ãã¬ãŒãžããããŠã³ããŒããããã¡ã€ã«ã§ããŒã«ã«ã¹ãã¬ãŒãžã®ãã¡ã€ã«ãæŽæ°ããã¿ã¹ã¯ãå¯äžã®UpdateFileFromServiceïŒstring fileIdïŒã¡ãœãããæã€FileUpdaterã¯ã©ã¹ããããšããŸãã
ãªã¹ããããããããã«ãUpdateFileFromServiceã¡ãœããã¯FileManagerã¯ã©ã¹ãšStorageServiceã¯ã©ã¹ã䜿çšããŠãããŒã«ã«ããã³ãªã¢ãŒãã®ãªããžããªã«ã¢ã¯ã»ã¹ããŸãã FileManagerã¯ã©ã¹ãšStorageServiceã¯ã©ã¹ã®ã¡ãœããã®å®è£
ãæå³çã«åŒçšããããšã¯ãããŸããããããã£ãŠããããã®ã€ã³ã¿ãŒãã§ã€ã¹ãç¥ãã ãã§æžã¿ãŸãã
public class FileUpdater
{
private StorageService storageService;
public StorageService Service
{
get
{
if (storageService == null )
{
storageService = new StorageService();
}
return storageService;
}
}
public void UpdateFileFromService( string fileId)
{
if ( string .IsNullOrEmpty(fileId))
{
throw new Exception( "fileId is empty" );
}
var fileManager = new FileManager();
string localFileHash = fileManager.GetFileHash(fileId);
string remoteFileHash = Service.GetFileHash(fileId);
if (localFileHash != remoteFileHash)
{
FileStream file = Service.DownloadFile(fileId);
fileManager.SaveFile(fileId, remoteFileHash, file);
}
}
}
public class FileManager
{
public string GetFileHash( string fileId)
{
throw new NotImplementedException();
}
public void SaveFile( string fileId, string remoteFileHash, FileStream file)
{
throw new NotImplementedException();
}
}
public class StorageService
{
public string GetFileHash( string fileId)
{
throw new NotImplementedException();
}
public FileStream DownloadFile( string fileId)
{
throw new NotImplementedException();
}
}
* This source code was highlighted with Source Code Highlighter .
UpdateFileFromServiceã¡ãœããã®æ¬¡ã®ããŒãžã§ã³ã¯
æããã§ãã
- fileId-空ã®æåå
- ããŒã«ã«ãšãªã¢ãŒãã®ãã¡ã€ã«ããã·ã¥ã¯åäžã§ã¯ãããŸãã
- ããŒã«ã«ãšãªã¢ãŒãã®ãã¡ã€ã«ããã·ã¥ã¯åäžã§ã
FileUpdaterã¯ã©ã¹ã®ãã®å®è£
ã«ã¯åŠçãå«ãŸããŠããªããããæ®ãã®ç¶æ³ã¯èæ
®ããŸããã
æåã®ã±ãŒã¹ã®ãã¹ãå®è£
ã¯ããã¹ããã¬ãŒã ã¯ãŒã¯ã®æšæºããŒã«ã䜿çšããåæ©çãªãã®ã§ãããããMicrosoft.Molesã䜿çšããŠãªãã·ã§ã³2ããã³3ã®ãã¹ããäœæããŠã¿ãŸãããã
ã»ããããã³ã¹ã¿ãã¯ã©ã¹ã®çæ
ãŸããã¯ã©ã¹ãå«ãã©ã€ãã©ãªã®Moledã¢ã»ã³ããªãçæããå¿
èŠããããŸããã¯ã©ã¹ã®ããžãã¯ã¯ãã«ã¹ã¿ã ããªã²ãŒãïŒãã以å€ã®å Žåã¯ã¹ã¿ãã¡ãœããïŒã«çœ®ãæããŸãã ããã¯ãMole.exeã䜿çšããã³ãã³ãã©ã€ã³ã䜿çšããŠå®è¡ã§ããŸããmole.exeã¯ãMicrosoft.Molesã®ã€ã³ã¹ããŒã«ããã±ãŒãžã«å«ãŸããŠãããVisual Studio 2008/2010ã€ã³ã¿ãŒãã§ã€ã¹ããã䜿çšã§ããŸãã 2çªç®ã®æ¹æ³ã䜿çšããŸãã
ã芧ã®ãšãããMicrosoft.Molesãã€ã³ã¹ããŒã«ãããšãåç
§ã¢ã»ã³ããªçšã®æ°ããã³ã³ããã¹ãã¡ãã¥ãŒé
ç®
ãAdd Moles Assemblyãã衚瀺ãããŸããïŒå³1ïŒã
ClassLibrary1ã¢ã»ã³ããªã§ãã®ã³ãã³ããå®è¡ãããšã
ClassLibrary1.molesãã¡ã€ã«ã®æ°ããã°ã«ãŒããååŸãããŸãïŒå³2ïŒãããããClassLibrary1.molesãã¡ã€ã«ã¯Moledã¢ã»ã³ããªèšè¿°åã§ãããçæçšã®ãã©ã¡ãŒã¿ãŒãå«ã¿ãå¿
èŠã«å¿ããŠå€æŽã§ããŸãã æ®ãã®ãã¡ã€ã«ã¯åãã«ãã§èªåçã«åçæãããç·šéããæå³ã¯ãããŸããã
Microsoft.Molesã§ã¯ã2ã€ã®ã¿ã€ãã®ä»£æ¿ã¯ã©ã¹ã䜿çšã§ããŸã-ã¹ã¿ãïŒã¢ãŒã«ã
- ã¹ã¿ãã¯ãæœè±¡ãä»®æ³ã¡ãœãããããã³ã¡ã³ããŒã€ã³ã¿ãŒãã§ã€ã¹ã®å®è£
ãåœè£
ãã軜éã®åé¢ãã¬ãŒã ã¯ãŒã¯ãæäŸããŸãã ã€ã³ã¿ãŒãã§ã€ã¹ã®å®è£
ã§ããä»®æ³ã¡ãœãããŸãã¯ã¡ã³ããŒã®ã¹ããŒãã£ã³ã°ã«é©ããŠããŸã
- Moleã¯åŒ·åãªåŒã³åºããªãã€ã¬ã¯ããã¬ãŒã ã¯ãŒã¯ã䜿çšããŸãããã®ãã¬ãŒã ã¯ãŒã¯ã¯ãã³ãŒããããã¡ã€ã©ãŒAPIã䜿çšããŠã䜿çšæžã¿ã¯ã©ã¹ãžã®åŒã³åºããã€ã³ã¿ãŒã»ããããåŒã³åºããåœã®ãªããžã§ã¯ãã«ãªãã€ã¬ã¯ãããŸãã ã¯ã©ã¹ã®ä»»æã®ã¡ã³ããŒã«ã³ãŒã«ããªãã€ã¬ã¯ãã§ããŸã
å人çã«ã¯ãäž¡æ¹ã®ãªãã·ã§ã³ãåãããã«äŸ¿å©ã§ããããšãå€æãã䜿çšã«æ¯éã¯ãããŸããã§ããã
çæãããã¢ã»ã³ããªClassLibrary1.Moles.dllã«ã¯ãããã©ã«ãã§ãã¢ã»ã³ããªClassLibrary1.dllã«å«ãŸããåã¯ã©ã¹ã®ã¯ã©ã¹ã®ãã¢MïŒ
ClassNameïŒ
ãšSïŒ
ClassNameïŒ
ãå«ãŸããŠããŸãã
ããã§ïŒ
- MïŒ
ClassNameïŒ
-ã»ããã¯ã©ã¹
- SïŒ
ClassNameïŒ
-ã¹ã¿ãã¯ã©ã¹
å¿
èŠã«å¿ããŠã
ClassLibrary1.molesãã¡ã€ã«ã«å€æŽãå ããããšã§ãç¹å®ã®ã¯ã©ã¹ã«å¯ŸããŠã®ã¿MolesãŸãã¯Stubãçæãããããã«ãïŒçææéã倧å¹
ã«ççž®ããŸãïŒãMoleãŸãã¯Stubã䜿çšããŠããªãå Žåã¯çæãç¡å¹ã«ãããããã®ä»ãèšå®ãããã§ããŸãçæãã©ã¡ãŒã¿ïŒã€ã³ããªãžã§ã³ã¹ã¯ãæå¹ãªãã©ã¡ãŒã¿ãšãã®ç®çã®ãªã¹ããæããŠãããŸãïŒã moles.exeã³ãã³ãã©ã€ã³ã䜿çšããŠçæãããšãã«ClassLibrary1.moles
ãã¡ã€ã«ã䜿çšããããšãã§ããŸãïŒmoles.exeãã©ã¡ãŒã¿ãŒã®ãªã¹ãã¯ããã©ã¡ãŒã¿ãŒãªãã§moles.exeãå®è¡ããããšã§ç¢ºèªã§ããŸãïŒã
Moleã¯ã©ã¹ã䜿çšãã
MolesãšStubsã®äœ¿çšã¯ãã䌌ãŠããããããã®èšäºã§ã¯Microsoft.Molesã®ãã¹ãŠã®æ©èœã«ã€ããŠå®å
šã«èª¬æãããµããããŠããŸãããMoleã®äœ¿çšäŸã®ã¿ãåãäžããŸãã
äŸãããæ確ã«ããããã«ãçæãããMoleããã³Stubã¯ã©ã¹ã®ã¡ã³ããŒã®åœåæ©èœã«ããã«æ³šæããŸããæåã«å
ã®ã¯ã©ã¹ã®ã¡ã³ããŒã®ååãæ¥ãŠã次ã«æž¡ããããã©ã¡ãŒã¿ãŒã®ååãããã«è¿œå ãããŸãã ããšãã°ã
MFileManager.AllInstances.GetFileHashStringã¯
ãFileManager.GetFileHashïŒstring fileIdïŒã¡ãœããã
ãªãŒããŒã©ã€ãããããããã£ã§ãã ãã®ã¹ã¿ã€ã«ã¯ããªãŒããŒããŒããããã¡ãœããã«å¯ŸããŠçæãããã¡ã³ããŒã®äžææ§ã確ä¿ããå¿
èŠæ§ã«é¢é£ããŠãããšæããŸãã
Moleã䜿çšãããšãã¯ã©ã¹ã®ã¡ãœãããšããããã£ãããŸããŸãªæ¹æ³ã§çœ®ãæããããšãã§ããŸãã
- ã¯ã©ã¹ã®ãã¹ãŠã®ã€ã³ã¹ã¿ã³ã¹ã眮ãæãã
MFileManager.AllInstances.GetFileHashString = (fileManager, fileId) =>
{
// GetFileHash(string fileId) FileManager
return Guid .NewGuid().ToString();
};
* This source code was highlighted with Source Code Highlighter .
éçã¯ã©ã¹ãšéçããããã£ã®çœ®æã¯ã AllInstancesãæå®ããªãå Žåã«ã®ã¿åæ§ã«å®è¡ãããŸãã
- ã¯ã©ã¹ã®ç¹å®ã®ã€ã³ã¹ã¿ã³ã¹ã眮ãæãã
var storageServiceMole = new MStorageService();
storageServiceMole.GetFileHashString = (fileId) =>
{
// GetFileHash(string fileId) storageService StorageService
return testRemoteHash;
};
var storageService = storageServiceMole.Instanc
* This source code was highlighted with Source Code Highlighter .
ãŸãã¯ãåæåãããã¯ã©ã¹ãªããžã§ã¯ãã®ã³ã³ã¹ãã©ã¯ã¿ãŒã§è»¢éããŸã
var storageService = new StorageService();
var storageServiceMole = new MStorageService(storageService);
storageServiceMole.GetFileHashString = (fileId) =>
{
// GetFileHash(string fileId) storageService StorageService
return testRemoteHash;
};
* This source code was highlighted with Source Code Highlighter .
- Moleã¯ã©ã¹ã®å¿
èŠãªã¡ã³ããŒãã¯ã©ã¹ã®ãã¹ãŠã®ã€ã³ã¹ã¿ã³ã¹ã®ãªããžã§ã¯ããšãªãŒããŒã©ãããããããã«ãã¯ã©ã¹ã³ã³ã¹ãã©ã¯ã¿ãŒã眮ãæããŸãã
MFileUpdater.Constructor = (@ this ) =>
{
var mole = new MFileUpdater(@ this );
mole.ServiceGet = (x) => initializedServiceInstance;
};
* This source code was highlighted with Source Code Highlighter .
- ã¯ã©ã¹ã®å
ã®å®è£
ãããã«åç
§ãã眮æã å
ã®ã¯ã©ã¹ã¡ãœãããåŒã³åºãåã«çä¿¡å€ã®ãã§ãã¯ãè¡ãå¿
èŠãããå Žåã¯ã次ã®ã¡ãœããã䜿çšããŸã
var fileUpdaterMole = new MFileUpdater() { InstanceBehavior = MoleBehaviors.Fallthrough };
fileUpdaterMole.UpdateFileFromServiceString = (y) =>
{
fileUpdaterMole.UpdateFileFromServiceString = null ; //
fileUpdaterMole.Instance.UpdateFileFromService(y); // FileUpdater.UpdateFileFromService
};
fileUpdaterMole.Instance.UpdateFileFromService( Guid .NewGuid().ToString());
* This source code was highlighted with Source Code Highlighter .
æããã«ãã©ã ãåŒã§ã¯ãçä¿¡å€ã®ããããçš®é¡ã®ã¢ãµãŒããã§ãã¯ãå®è¡ãããã
æå®ã®çµæãè¿ã£ãŠããä¿ææ§ã
InstanceBehavior
ãŸãã代ããã®ããªã²ãŒããæ瀺çã«æå®ããŠããªãã¯ã©ã¹ã¡ã³ããŒã®åäœãæå®ããå¯èœæ§ã泚ç®ã«å€ããŸãã Moleãçæããåã¯ã©ã¹ã«ã¯InstanceBehaviorããããã£ãããã次ã®å€ãåãããšãã§ããŸã
- MoleBehaviors.DefaultValue-ã¯ã©ã¹ã®é眮æã¡ã³ããŒã¯ç©ºã®ããªã²ãŒãã«çœ®ãæããããè¿ãããçµæã®åã®ããã©ã«ãå€ãè¿ããŸã
var storageService = new StorageService();
var storageServiceMole = new MStorageService(storageService) { InstanceBehavior = MoleBehaviors.DefaultValue };
// null;
var result = storageService.GetFileHash( Guid .NewGuid().ToString());
* This source code was highlighted with Source Code Highlighter .
- MoleBehaviors.NotImplemented-é眮æã¡ã³ããŒã«ã¢ã¯ã»ã¹ãããšã NotImplementedExceptionãã¹ããŒãããŸã
var storageService = new StorageService();
var storageServiceMole = new MStorageService(storageService) { InstanceBehavior = MoleBehaviors.NotImplemented };
// NotImplementedException
storageService.GetFileHash( "328576BA-7345-4847-84AC-170EF03FFA7A" );
* This source code was highlighted with Source Code Highlighter .
- MoleBehaviors.Fallthrough-é眮æã¡ã³ããŒãžã®åŒã³åºãã¯ã眮ãæããããã¯ã©ã¹ã®å
ã®å®è£
ã«åŸã£ãŠåŠçãããŸã
var storageService = new StorageService();
var storageServiceMole = new MStorageService() {InstanceBehavior = MoleBehaviors.Fallthrough};
// StorageService.GetFileHash(string fileId)
storageService.GetFileHash( "328576BA-7345-4847-84AC-170EF03FFA7A" );
* This source code was highlighted with Source Code Highlighter .
- MoleBehaviors.Current -Moleã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãã¯ã©ã¹ãšéè€ããåŸã®é眮æã¡ãœããã®ããã©ã«ãã®åäœã é眮æã¡ã³ããŒãåŒã³åºããããšã MoleNotImplementedExceptionãã¹ããŒãããŸã
var storageService = new StorageService();
var storageServiceMole = new MStorageService() {InstanceBehavior = MoleBehaviors.Current};
// MoleNotImplementedException
storageService.GetFileHash( "328576BA-7345-4847-84AC-170EF03FFA7A" );
* This source code was highlighted with Source Code Highlighter .
- MoleBehaviors.CurrentProxyã¯ãéè€ããã¯ã©ã¹ã€ã³ã¹ã¿ã³ã¹ã®éè€ããªããã¹ãŠã®ã¡ã³ããŒã®çŸåšã®åäœãæ®ããŸã
var storageService = new StorageService();
var storageServiceMole1 = new MStorageService(storageService) { InstanceBehavior = MoleBehaviors.NotImplemented };
storageServiceMole1.GetFileHashString = (fileId) =>
{
return Guid .NewGuid().ToString();
};
var storageServiceMole2 = new MStorageService(storageService) { InstanceBehavior = MoleBehaviors.CurrentProxy };
storageServiceMole2.DownloadFileString = (fileId) =>
{
return testFileStream;
};
// storageServiceMole1.GetFileHashString
storageService.GetFileHash( "328576BA-7345-4847-84AC-170EF03FFA7A" );
// storageServiceMole2.GetFileHashString
storageService.DownloadFile( "328576BA-7345-4847-84AC-170EF03FFA7A" );
* This source code was highlighted with Source Code Highlighter .
- åäœã®ã«ã¹ã¿ã å®è£
-IMoleBehaviorã€ã³ã¿ãŒãã§ãŒã¹ã®ç¬èªã®å®è£
ãäœæããå Žåã«å¯èœ
ãã¹ãå®è£
äŸ
ããã¯ãããŒã«ã«ãã¡ã€ã«ãšãªã¢ãŒããã¡ã€ã«ã®ããã·ã¥ãç°ãªãå Žåã®ç¶æ³ã®å®éã®ãã¹ãã§ãã
///
/// ,
///
[TestMethod]
[HostType( "Moles" )]
public void TestUpdateFileNonMatchedHash()
{
var callOrder = 0; //
var testFileId = Guid .NewGuid().ToString();
var testLocalHash = Guid .NewGuid().ToString();
var testRemoteHash = Guid .NewGuid().ToString();
var testFileStream = new FileStream ( @"c:\testfile.txt" , FileMode .OpenOrCreate);
var storageServiceMole = new MStorageService() { InstanceBehavior = MoleBehaviors.Fallthrough };
// GetFileHash StorageService
storageServiceMole.GetFileHashString = (fileId) =>
{
Assert.AreEqual(1, callOrder++); //
Assert.AreEqual(testFileId, fileId);
Assert.AreNotEqual(testLocalHash, testRemoteHash);
return testRemoteHash;
};
storageServiceMole.DownloadFileString = (fileId) =>
{
Assert.AreEqual(2, callOrder++);
Assert.AreEqual(testFileId, fileId);
return testFileStream;
};
// FileManager.
//MFileManager.AllInstances FileManager UpdateFile
// ,
// FileManager
MFileManager.AllInstances.GetFileHashString = (fileManager, fileId) =>
{
Assert.AreEqual(0, callOrder++);
Assert.AreEqual(testFileId, fileId);
return Guid .NewGuid().ToString();
};
MFileManager.AllInstances.SaveFileStringStringFileStream = (fileManager, fileId, fileHash, fileStream) =>
{
Assert.AreEqual(3, callOrder++);
Assert.AreEqual(testFileId, fileId);
Assert.AreEqual(testRemoteHash, fileHash);
Assert.AreSame(testFileStream, fileStream);
};
var fileUpdaterMole = new MFileUpdater
{
InstanceBehavior = MoleBehaviors.Fallthrough,
// getter FileUpdater.Service , moled StorageService
ServiceGet = () => storageServiceMole.Instance
};
var fileUpdater = fileUpdaterMole.Instance;
fileUpdater.UpdateFileFromService(testFileId);
}
* This source code was highlighted with Source Code Highlighter .
2çªç®ã®ç¶æ³-ããã·ã¥ãåäžã®å Žåããã¹ããèªåã§å®è£
ããŠã¿ãŠãã ããã
è¿œå æ
å ±
- ã»ããåã¯ããã«ãã¹ã¬ãããã¹ãã§ã¯äœ¿çšã§ããŸããã
- ã¢ã«ã䜿çšããŠãäžéšã®çš®é¡ã®mscorlibã眮ãæããããšã¯ã§ããŸããã
- Microsoft.Molesã䜿çšãããã¹ãã¯ã Moleã«ãã£ãŠã€ã³ã¹ãã«ã¡ã³ãåããå¿
èŠããããŸã-MSTestã®å Žåãããã¯HostTypeïŒ "Moles"ïŒãã¹ãã¡ãœããã®å±æ§ã§ãã ä»ã®ãã¹ããã¬ãŒã ã¯ãŒã¯ã®å Žåãmoles.runner.exeã䜿çšããŠãã¹ããå®è¡ããå¿
èŠããããŸãïŒnunitã®å Žåã moles.runner.exe "yourTestDLLName" /runner:nunit-console.exeã®ããã«ãªããŸãïŒããããã£ãŠãGUIã䜿çšããŠãã¹ããã¬ãŒã ã¯ãŒã¯ãæäœãããã¡ã³ãå®è¡ããå¿
èŠããããŸãmole.exeã«ããã·ã§ã«
- ã»ããã«ãã£ãŠèšæž¬ããããã¹ããåŒã³åºãããã®Nantã¿ã¹ã¯ã®èšå®äŸ
< target name ="runTests" >
< exec basedir ="${build.dir.unittests}" program ="${microsoft.moles.dir}moles.runner.exe" >
< arg value ="/Runner:${nunit.console.dir}nunit-console.exe" ></ arg >
< arg value ="${build.dir.unittests}My.UnitTests.dll" ></ arg >
</ exec >
</ target >
* This source code was highlighted with Source Code Highlighter .
ãšãããããããçµäºããŸãããã®æ
å ±ãããããããã°ç¶ç¶ãããããããŸããã
ãœãŒã¹
Microsoft Molesãªãã¡ã¬ã³ã¹ããã¥ã¢ã«
NUnitãNAntãããã³.NET 3.5ã§MS Molesã䜿çšãã
MSDN-ã¢ã¬ãã¬ãŒã ã¯ãŒã¯