Unit Testing Registry access with RhinoMocks and SystemWrapper
As mentioned in the previous Unit Test post (Unit Testing Registry access with RhinoMocks and SystemWrapper) it important to be able to Unit Test code that access the registry without really accessing the registry.
Prerequisites
- It is assumed you have read the previous post, Unit Testing Registry access with RhinoMocks and SystemWrapper and you are ready for the Registry Key example.
- It is assumed you have already heard of Mocking and interface-based design (see this post: Architecting a large application with interface-based architecture) that allows for mocking to occur.
Step 1 – Download SystemWrapper
You may have already done this when reading the previous post, if not do so now.
- Go to https://github.com/jozefizso/SystemWrapper and download the latest dll or the latest source or add the SystemWrapper.Interfaces and SystemWrapper.Wrappers By jozef.izso
- Copy the SystemInterface.dll and SystemWrapper.dll into your project (perhaps you already have a libs directory).
- Add references to these two dlls in your release code.
- Add a reference only to SystemInterface.dll in your Unit Test project.
Step 2 – Change your code to use Interfaces and Wrappers
Ok, so here we are going to start with an example. Here is an object that I wrote myself that uses the registry to check on which versions of .NET Framework are installed. It is currently using Registry and RegistryKey directly.
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | using System; using Microsoft.Win32; namespace SystemInfo { public class DotNetFramework { #region Constant members public const string REGPATH_DOTNET11_VERSION = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" ; public const string REGPATH_DOTNET20_VERSION = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" ; public const string REGPATH_DOTNET30_VERSION = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0" ; public const string REGPATH_DOTNET35_VERSION = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" ; public const string REGPATH_DOTNET40_VERSION_CLIENT = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client" ; public const string REGPATH_DOTNET40_VERSION = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" ; #endregion #region Properties /// <summary> /// This returns true if .NET 4 Full is installed. /// </summary> public bool FoundDotNet4 { get { return HasDotNetFramework(REGPATH_DOTNET40_VERSION, 0); } } /// <summary> /// This returns true if .NET 4 Client is installed. /// </summary> public bool FoundDotNet4Client { get { return HasDotNetFramework(REGPATH_DOTNET40_VERSION_CLIENT, 0); } } /// <summary> /// This returns true if .NET 3.5 with SP1 is installed. /// </summary> public bool FoundDotNet35SP1 { get { return HasDotNetFramework(REGPATH_DOTNET35_VERSION, 1); } } /// <summary> /// This returns true if .NET 3.5 is installed. /// </summary> public bool FoundDotNet35 { get { return HasDotNetFramework(REGPATH_DOTNET35_VERSION, 0); } } /// <summary> /// This returns true if .NET 3.0 with SP2 is installed. /// </summary> public bool FoundDotNet30SP2 { get { return HasDotNetFramework(REGPATH_DOTNET30_VERSION, 2); } } /// <summary> /// This returns true if .NET 3.0 with SP1 is installed. /// </summary> public bool FoundDotNet30SP1 { get { return HasDotNetFramework(REGPATH_DOTNET30_VERSION, 1); } } /// <summary> /// This returns true if .NET 3.0 is installed. /// </summary> public bool FoundDotNet30 { get { return HasDotNetFramework(REGPATH_DOTNET30_VERSION, 0); } } /// <summary> /// This returns true if .NET 2.0 with SP2 is installed. /// </summary> public bool FoundDotNet20SP2 { get { return HasDotNetFramework(REGPATH_DOTNET20_VERSION, 2); } } /// <summary> /// This returns true if .NET 2.0 with SP1 is installed. /// </summary> public bool FoundDotNet20SP1 { get { return HasDotNetFramework(REGPATH_DOTNET20_VERSION, 1); } } /// <summary> /// This returns true if .NET 2.0 is installed. /// </summary> public bool FoundDotNet20 { get { return HasDotNetFramework(REGPATH_DOTNET20_VERSION, 0); } } /// <summary> /// This returns true if .NET 1.1 is installed. /// </summary> public bool FoundDotNet11 { get { return HasDotNetFramework(REGPATH_DOTNET11_VERSION, 0); } } #endregion public bool HasDotNetFramework( string dotNetRegPath, int expectedServicePack) { bool retVal = false ; try { RegistryKey localKey = Registry.LocalMachine.OpenSubKey(dotNetRegPath); if (localKey != null ) { int ? isInstalled = localKey.GetValue( "Install" ) as int ?; if (isInstalled != null ) { if (isInstalled == 1) { if (expectedServicePack > 0) { if (( int )localKey.GetValue( "SP" ) >= expectedServicePack) retVal = true ; } else { retVal = true ; } } } } return retVal; } catch { return retVal; } } } } |
- Find any place in your code where you use touch the system. For example, if you touch the registry, find any place you call Registry or RegistryKey objects or reference Microsoft.Win32.
Hint: Use the search feature in your IDE. - Replace the reference with a reference to SystemWrapper and SystemInterfaces. For example, if replacingrRegistry code,
SystemInterface.Microsoft.Win32
SystemWrapper.Microsoft.Win32In the DotNetFramework.cs file above, there is a using statement to Microsoft.Win32. Our new using statements will be:123using
System;
using
SystemInterface.Microsoft.Win32;
using
SystemWrapper.Microsoft.Win32;
- For any object that touches the system, change that object to be instantiated using the interface. For example, replace RegistryKey objects with IRegistryKey objects.In the DotNetFramework.cs file, there is only one single line that needs to change to do this, line 113.
This line…1RegistryKey localKey = Registry.LocalMachine.OpenSubKey(dotNetRegPath);
…changes to this line.
1IRegistryKey localKey =
new
RegistryWrap().LocalMachine.OpenSubKey(dotNetRegPath);
Step 3 – Allow for replacing the IRegistryKey objects
Create a property or function that allows you to replace the object you are mocking so you can pass in an object that is mocked.
This is done for you to some extent for registry access by using the IAccessTheRegistry interface.
- Implementing the IAccessTheRegistry interface from the SystemInterface.Microsoft.Win32 namespace. You can add the following code to the DotNetFramework object.
010203040506070809101112131415161718
#region IAccessTheRegistry Members
public
IRegistryKey BaseKey
{
get
{
if
(
null
== _BaseKey)
_BaseKey =
new
RegistryWrap().LocalMachine;
return
_BaseKey;
}
}
private
IRegistryKey _BaseKey;
public
void
ChangeBaseKey(IRegistryKey inBaseKey)
{
_BaseKey = inBaseKey;
}
#endregion
Note: Notice that the Property is read only. Instead of enabling the set ability, a function is created to allow you to change the IRegistryKey instance. This is by design and keeps you from making a mistake such as BaseKey = BaseKey.OpenSubkey(“…”) and replacing the BaseKey.
Note: You may want to look into the “Factory” design pattern.
- Change line 113 to use the BaseKey.
1
IRegistryKey localKey = BaseKey.OpenSubKey(dotNetRegPath);
Step 4 – Download and Reference RhinoMocks in your Unit Test
You may have already done this when reading the previous post, if not do so now.
In this step you need to download RhinoMocks and reference it with your Unit Test project. If you don’t have a test project, create one. Your release project won’t need it.
- Go to http://hibernatingrhinos.com/open-source/rhino-mocks and download RhinoMocks.
- Add RhinoMocks.dll
Step 5 – Mock IRegistryKey in your Unit Test
For this, you have to understand and know how to use RhinoMocks and this takes some learning. For this we need an example.
- Add a new class to your test project.
- Add a using statement:
1
using
Rhino.Mocks;
- Add code to create the Mock objects.I put these in separate functions because every test needed the exact same code.
- Mock the HKLM key and any calls made using it.
- Mock the registry sub key and any calls made using it.
010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778#region Test Helper Functions
/// <summary>
/// Mock to pretend to be this registry subkey:
/// Hive: HKLM
///
/// No stubbing is preconfigured
/// </summary>
/// <returns>IRegistryKey</returns>
private
IRegistryKey CreateMockOfHKLM()
{
// Mock to pretend to be this registry key:
// Hive: HKLM
IRegistryKey hklmMock = MockRepository.GenerateMock<IRegistryKey>();
hklmMock.Stub(x => x.Name).Return(
"HKEY_LOCAL_MACHINE"
);
return
hklmMock;
}
/// <summary>
/// Mock to pretend to be this registry subkey:
/// Hive: HKLM
///
/// It has stubs preconfigured
/// </summary>
/// <returns>IRegistry that is a mock of HKLM with IIS Version stubs.</returns>
private
IRegistryKey CreateDotNetRegistryOpenSubKeyMock(
string
inSubkey,
bool
inIsInstalled =
true
,
int
? inSP =
null
)
{
// Mock to pretend to be this registry subkey:
// Hive: HKLM
IRegistryKey hklmMock = CreateMockOfHKLM();
// Allow to test a key that doesn't exist (null) or one that does.
IRegistryKey dotNetMock =
null
;
if
(inIsInstalled)
{
dotNetMock = CreateDotNetRegistryGetValueMock(inSubkey, inSP);
}
// Stubs using hklmMock to open and return this registry key
// and return dotNetMock:
hklmMock.Stub(x => x.OpenSubKey(inSubkey)).Return(dotNetMock);
return
hklmMock;
}
private
IRegistryKey CreateDotNetRegistryGetValueMock(
string
inSubkey,
int
? inSP)
{
// Mock to pretend to be this subkey passed in:
// Hive: HKLM
// SubKey: HKEY_LOCAL_MACHINE\" + inSubkey
IRegistryKey dotNetMock = MockRepository.GenerateMock<IRegistryKey>();
dotNetMock.Stub(x => x.Name).Return(
@"HKEY_LOCAL_MACHINE\"
+ inSubkey);
// Stubs checking the available registry properties:
// Hive: HKLM
// SubKey: HKEY_LOCAL_MACHINE\" + inSubkey
// Properties: "Install", "SP"
dotNetMock.Stub(x => x.GetValueNames()).Return(
new
String[] {
"Install"
,
"SP"
});
// Stubs checking this registry:
// Hive: HKLM
// SubKey: HKEY_LOCAL_MACHINE\" + inSubkey
// Property: Install
// Value: 1 - If not installed the whole key shouldn't even exist so it should always be 1
dotNetMock.Stub(x => x.GetValue(
"Install"
)).Return(1);
if
(
null
!= inSP)
{
// Stubs checking this registry:
// Hive: HKLM
// SubKey: HKEY_LOCAL_MACHINE\" + inSubkey
// Property: SP
// Value: null or 1, 2, 3, ...
dotNetMock.Stub(x => x.GetValue(
"SP"
)).Return(inSP);
}
return
dotNetMock;
}
#endregion
- Now go forth and mock all your code that uses the Registry.
Here is the final test file.
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 | using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Rhino.Mocks; using SystemInfo; using SystemInterface.Microsoft.Win32; namespace DotNetFrameworkTest { /// <summary> ///This is a test class for DotNetFrameworkTest and is intended ///to contain all DotNetFrameworkTest Unit Tests ///</summary> [TestClass()] public class DotNetFrameworkTest { #region Constructor tests /// <summary> ///A test for DotNetFramework Constructor ///</summary> [TestMethod()] public void DotNetFrameworkConstructorTest() { DotNetFramework target = new DotNetFramework(); Assert.IsNotNull(target); } #endregion #region IAccessTheRegistry interface items test /// <summary> ///A test for ChangeBaseKey ///</summary> [TestMethod()] public void BaseKeyAndChangeBaseKeyTest() { DotNetFramework target = new DotNetFramework(); // TODO: Initialize to an appropriate value IRegistryKey defaultBaseKey = target.BaseKey; IRegistryKey inBaseKey = MockRepository.GenerateMock<IRegistryKey>(); target.ChangeBaseKey(inBaseKey); // Make sure the newly assigned is not the same as the original Assert.AreNotEqual(target.BaseKey, defaultBaseKey); // Make sure that the assignment worked Assert.AreEqual(target.BaseKey, inBaseKey); } #endregion #region FoundDotNet11 tests /// <summary> ///A test for FoundDotNet11 mocking the key so it should return true ///</summary> [TestMethod()] public void FoundDotNet11_Test_KeyExists() { bool isInstalled = true ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET11_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = true ; bool actual = target.FoundDotNet11; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET11_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET11_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasNotCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet11 mocking the lack of a key so it should return false ///</summary> [TestMethod()] public void FoundDotNet11_Test_KeyDoesNotExist() { bool isInstalled = false ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET11_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet11; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET11_VERSION)); Assert.IsNull(key.OpenSubKey(DotNetFramework.REGPATH_DOTNET11_VERSION)); } #endregion #region FoundDotNet20 tests /// <summary> ///A test for FoundDotNet20 mocking the key so it should return true ///</summary> [TestMethod()] public void FoundDotNet20_Test_KeyExists() { bool isInstalled = true ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET20_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = true ; bool actual = target.FoundDotNet20; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasNotCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet20 mocking the lack of a key so it should return false ///</summary> [TestMethod()] public void FoundDotNet20_Test_KeyDoesNotExist() { bool isInstalled = false ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET20_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet20; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION)); Assert.IsNull(key.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION)); } #endregion #region FoundDOtNet20SP1 tests /// <summary> ///A test for FoundDotNet20SP1 mocking the key so it should return true ///</summary> [TestMethod()] public void FoundDotNet20SP1_Test_KeyExistsWithSP1() { bool isInstalled = true ; int ? sp = 1; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET20_VERSION, isInstalled, sp); target.ChangeBaseKey(key); bool expected = true ; bool actual = target.FoundDotNet20SP1; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet20SP1 mocking the key so it should return false due to no service pack ///</summary> [TestMethod()] public void FoundDotNet20SP1_Test_KeyExistsWithoutSP1() { bool isInstalled = true ; int ? sp = null ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET20_VERSION, isInstalled, sp); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet20SP1; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet20SP1 mocking the lack of a key so it should return false ///</summary> [TestMethod()] public void FoundDotNet20SP1_Test_KeyDoesNotExist() { bool isInstalled = false ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET20_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet20SP1; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION)); Assert.IsNull(key.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION)); } #endregion #region FoundDOtNet20SP2 tests /// <summary> ///A test for FoundDotNet20SP2 mocking the key so it should return true ///</summary> [TestMethod()] public void FoundDotNet20SP2_Test_KeyExistsWithSP2() { bool isInstalled = true ; int ? sp = 2; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET20_VERSION, isInstalled, sp); target.ChangeBaseKey(key); bool expected = true ; bool actual = target.FoundDotNet20SP2; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet20SP2 mocking the key so it should return false because it only has SP1 ///</summary> [TestMethod()] public void FoundDotNet20SP2_Test_KeyExistsWithSP1() { bool isInstalled = true ; int ? sp = 1; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET20_VERSION, isInstalled, sp); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet20SP2; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet20SP2 mocking the key so it should return false due to no service pack ///</summary> [TestMethod()] public void FoundDotNet20SP2_Test_KeyExistsWithoutSP() { bool isInstalled = true ; int ? sp = null ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET20_VERSION, isInstalled, sp); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet20SP2; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet20SP2 mocking the lack of a key so it should return false ///</summary> [TestMethod()] public void FoundDotNet20SP2_Test_KeyDoesNotExist() { bool isInstalled = false ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET20_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet20SP2; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION)); Assert.IsNull(key.OpenSubKey(DotNetFramework.REGPATH_DOTNET20_VERSION)); } #endregion #region FoundDotNet30 tests /// <summary> ///A test for FoundDotNet30 mocking the key so it should return true ///</summary> [TestMethod()] public void FoundDotNet30_Test_KeyExists() { bool isInstalled = true ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET30_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = true ; bool actual = target.FoundDotNet30; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasNotCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet30 mocking the lack of a key so it should return false ///</summary> [TestMethod()] public void FoundDotNet30_Test_KeyDoesNotExist() { bool isInstalled = false ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET30_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet30; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION)); Assert.IsNull(key.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION)); } #endregion #region FoundDOtNet30SP1 tests /// <summary> ///A test for FoundDotNet30SP1 mocking the key so it should return true ///</summary> [TestMethod()] public void FoundDotNet30SP1_Test_KeyExistsWithSP1() { bool isInstalled = true ; int ? sp = 1; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET30_VERSION, isInstalled, sp); target.ChangeBaseKey(key); bool expected = true ; bool actual = target.FoundDotNet30SP1; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet30SP1 mocking the key so it should return false due to no service pack ///</summary> [TestMethod()] public void FoundDotNet30SP1_Test_KeyExistsWithoutSP() { bool isInstalled = true ; int ? sp = null ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET30_VERSION, isInstalled, sp); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet30SP1; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet30SP1 mocking the lack of a key so it should return false ///</summary> [TestMethod()] public void FoundDotNet30SP1_Test_KeyDoesNotExist() { bool isInstalled = false ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET30_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet30SP1; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION)); Assert.IsNull(key.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION)); } #endregion #region FoundDOtNet30SP2 tests /// <summary> ///A test for FoundDotNet30SP2 mocking the key so it should return true ///</summary> [TestMethod()] public void FoundDotNet30SP2_Test_KeyExistsWithSP2() { bool isInstalled = true ; int ? sp = 2; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET30_VERSION, isInstalled, sp); target.ChangeBaseKey(key); bool expected = true ; bool actual = target.FoundDotNet30SP2; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet30SP2 mocking the key so it should return false because it only has SP1 ///</summary> [TestMethod()] public void FoundDotNet30SP2_Test_KeyExistsWithSP1() { bool isInstalled = true ; int ? sp = 1; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET30_VERSION, isInstalled, sp); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet30SP2; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet30SP2 mocking the key so it should return false due to no service pack ///</summary> [TestMethod()] public void FoundDotNet30SP2_Test_KeyExistsWithoutSP() { bool isInstalled = true ; int ? sp = null ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET30_VERSION, isInstalled, sp); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet30SP2; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet30SP2 mocking the lack of a key so it should return false ///</summary> [TestMethod()] public void FoundDotNet30SP2_Test_KeyDoesNotExist() { bool isInstalled = false ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET30_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet30SP2; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION)); Assert.IsNull(key.OpenSubKey(DotNetFramework.REGPATH_DOTNET30_VERSION)); } #endregion #region FoundDotNet35 tests /// <summary> ///A test for FoundDotNet35 mocking the key so it should return true ///</summary> [TestMethod()] public void FoundDotNet35_Test_KeyExists() { bool isInstalled = true ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET35_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = true ; bool actual = target.FoundDotNet35; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET35_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET35_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasNotCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet35 mocking the lack of a key so it should return false ///</summary> [TestMethod()] public void FoundDotNet35_Test_KeyDoesNotExist() { bool isInstalled = false ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET35_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet35; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET35_VERSION)); Assert.IsNull(key.OpenSubKey(DotNetFramework.REGPATH_DOTNET35_VERSION)); } #endregion #region FoundDOtNet35SP1 tests /// <summary> ///A test for FoundDotNet35SP1 mocking the key so it should return true ///</summary> [TestMethod()] public void FoundDotNet35SP1_Test_KeyExistsWithSP1() { bool isInstalled = true ; int ? sp = 1; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET35_VERSION, isInstalled, sp); target.ChangeBaseKey(key); bool expected = true ; bool actual = target.FoundDotNet35SP1; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET35_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET35_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet35SP1 mocking the key so it should return false due to no service pack ///</summary> [TestMethod()] public void FoundDotNet35SP1_Test_KeyExistsWithoutSP() { bool isInstalled = true ; int ? sp = null ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET35_VERSION, isInstalled, sp); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet35SP1; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET35_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET35_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet35SP1 mocking the lack of a key so it should return false ///</summary> [TestMethod()] public void FoundDotNet35SP1_Test_KeyDoesNotExist() { bool isInstalled = false ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET35_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet35SP1; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET35_VERSION)); Assert.IsNull(key.OpenSubKey(DotNetFramework.REGPATH_DOTNET35_VERSION)); } #endregion #region FoundDotNet 4 Full tests /// <summary> ///A test for FoundDotNet4 (full) mocking the key so it should return true ///</summary> [TestMethod()] public void FoundDotNet4_Full_Test_KeyExists() { bool isInstalled = true ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET40_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = true ; bool actual = target.FoundDotNet4; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET40_VERSION)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET40_VERSION); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasNotCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet4 (full) mocking the lack of a key so it should return false ///</summary> [TestMethod()] public void FoundDotNet4_Test_KeyDoesNotExist() { bool isInstalled = false ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET40_VERSION, isInstalled); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet4; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET40_VERSION)); Assert.IsNull(key.OpenSubKey(DotNetFramework.REGPATH_DOTNET40_VERSION)); } #endregion #region FoundDotNet4 Client tests /// <summary> ///A test for FoundDotNet4Client mocking the key so it should return true ///</summary> [TestMethod()] public void FoundDotNet4Client_Test_KeyExists() { bool isInstalled = true ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET40_VERSION_CLIENT, isInstalled); target.ChangeBaseKey(key); bool expected = true ; bool actual = target.FoundDotNet4Client; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET40_VERSION_CLIENT)); IRegistryKey innerkey = key.OpenSubKey(DotNetFramework.REGPATH_DOTNET40_VERSION_CLIENT); innerkey.AssertWasCalled(x => x.GetValue( "Install" )); innerkey.AssertWasNotCalled(x => x.GetValue( "SP" )); } /// <summary> ///A test for FoundDotNet4 (client) mocking the lack of a key so it should return false ///</summary> [TestMethod()] public void FoundDotNet4Client_Test_KeyDoesNotExist() { bool isInstalled = false ; DotNetFramework target = new DotNetFramework(); IRegistryKey key = CreateDotNetRegistryOpenSubKeyMock(DotNetFramework.REGPATH_DOTNET40_VERSION_CLIENT, isInstalled); target.ChangeBaseKey(key); bool expected = false ; bool actual = target.FoundDotNet4Client; Assert.AreEqual(actual, expected); key.AssertWasCalled(x => x.OpenSubKey(DotNetFramework.REGPATH_DOTNET40_VERSION_CLIENT)); Assert.IsNull(key.OpenSubKey(DotNetFramework.REGPATH_DOTNET40_VERSION_CLIENT)); } #endregion #region HasDotNetFrameworkTest /// <summary> ///A test for HasDotNetFramework ///</summary> [TestMethod()] public void HasDotNetFrameworkTest() { // No need to test this as all properties test this } #endregion #region Test Helper Functions /// <summary> /// Mock to pretend to be this registry subkey: /// Hive: HKLM /// /// No stubbing is preconfigured /// </summary> /// <returns>IRegistryKey</returns> private IRegistryKey CreateMockOfHKLM() { // Mock to pretend to be this registry key: // Hive: HKLM IRegistryKey hklmMock = MockRepository.GenerateMock<IRegistryKey>(); hklmMock.Stub(x => x.Name).Return( "HKEY_LOCAL_MACHINE" ); return hklmMock; } /// <summary> /// Mock to pretend to be this registry subkey: /// Hive: HKLM /// /// It has stubs preconfigured /// </summary> /// <returns>IRegistry that is a mock of HKLM with IIS Version stubs.</returns> private IRegistryKey CreateDotNetRegistryOpenSubKeyMock( string inSubkey, bool inIsInstalled = true , int ? inSP = null ) { // Mock to pretend to be this registry subkey: // Hive: HKLM IRegistryKey hklmMock = CreateMockOfHKLM(); // Allow to test a key that doesn't exist (null) or one that does. IRegistryKey dotNetMock = null ; if (inIsInstalled) { dotNetMock = CreateDotNetRegistryGetValueMock(inSubkey, inSP); } // Stubs using hklmMock to open and return this registry key // and return dotNetMock: hklmMock.Stub(x => x.OpenSubKey(inSubkey)).Return(dotNetMock); return hklmMock; } private IRegistryKey CreateDotNetRegistryGetValueMock( string inSubkey, int ? inSP) { // Mock to pretend to be this subkey passed in: // Hive: HKLM // SubKey: HKEY_LOCAL_MACHINE\" + inSubkey IRegistryKey dotNetMock = MockRepository.GenerateMock<IRegistryKey>(); dotNetMock.Stub(x => x.Name).Return( @"HKEY_LOCAL_MACHINE\" + inSubkey); // Stubs checking the available registry properties: // Hive: HKLM // SubKey: HKEY_LOCAL_MACHINE\" + inSubkey // Properties: "Install", "SP" dotNetMock.Stub(x => x.GetValueNames()).Return( new String[] { "Install" , "SP" }); // Stubs checking this registry: // Hive: HKLM // SubKey: HKEY_LOCAL_MACHINE\" + inSubkey // Property: Install // Value: 1 - If not installed the whole key shouldn't even exist so it should always be 1 dotNetMock.Stub(x => x.GetValue( "Install" )).Return(1); if ( null != inSP) { // Stubs checking this registry: // Hive: HKLM // SubKey: HKEY_LOCAL_MACHINE\" + inSubkey // Property: SP // Value: null or 1, 2, 3, ... dotNetMock.Stub(x => x.GetValue( "SP" )).Return(inSP); } return dotNetMock; } #endregion } } |
Return to C# Unit Test Tutorial
[…] https://www.rhyous.com/2011/11/04/unit-testing-registry-access-with-rhinomocks-and-systemwrapper/ […]
I cannot find the systeminterface.dll,can you help me please?
I think you have to check out the .NET 4 version and build it. I don't think the main developers of the project have created a release since I made the .NET 4 version and enhanced it some.
Thanks very much.I have got the dll,but when I changed the code like below.
IRegistryKey localKey = new RegistryWrap().LocalMachine.OpenSubKey(dotNetRegPath);
there is error as follows.
Error 1 Cannot implicitly convert type 'SystemWrapper.Microsoft.Win32.IRegistryKeyWrap' to 'SystemInterface.Microsoft.Win32.IRegistryKey'. An explicit conversion exists (are you missing a cast?) C:\VS2012\Unit Tests\MockRegistry\MockRegistry\DotNetFramework\DotNetFramework.cs 120 41 DotNetFramework
could you please give some tips?
It's OK now ,I changed the built dll and now it's OK
[...] Unit Testing Registry access with RhinoMocks and SystemWrapper [...]
Nice post. Keep spreading the word about SystemWrapper.
I hope to add a few more posts!