Last active
January 15, 2016 19:19
-
-
Save miklund/5787825a93d992802dcb to your computer and use it in GitHub Desktop.
2008-12-06 Mocking with Rhino Mocks 3.5
This file contains hidden or 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
# Title: Mocking with Rhino Mocks 3.5 | |
# Author: Mikael Lundin | |
# Link: http://blog.mikaellundin.name/2008/12/06/mocking-with-rhino-mocks.html |
This file contains hidden or 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
public class ArticleRepository | |
{ | |
private IDataAccess<Article> dataAccess; | |
private IValidate<Article> validator; | |
public ArticleRepository(IDataAccess<Article> dataAccess, IValidate<Article> validator) | |
{ | |
this.dataAccess = dataAccess; | |
this.validator = validator; | |
} | |
public void Save(Article article) | |
{ | |
// If validation is success, save article | |
if (validator.Validate(article)) | |
dataAccess.Save(article); | |
} | |
} |
This file contains hidden or 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
[Test(Description="Should not call save when validation fails")] | |
public void ShouldNotCallSaveWhenValidationFails() | |
{ | |
/* Setup */ | |
IDataAccess<Article> dataAccess = MockRepository.GenerateMock<IDataAccess<Article>>(); | |
IValidate<Article> validator = MockRepository.GenerateMock<IValidate<Article>>(); | |
Article article = new Article("My new article"); | |
/* Arrange */ | |
validator.Expect(va => va.Validate(article)).Return(false); | |
/* Act */ | |
new ArticleRepository(dataAccess, validator).Save(article); | |
/* Assert */ | |
dataAccess.AssertWasNotCalled(da => da.Save(article)); | |
} |
This file contains hidden or 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
[Test(Description = "Should save if article validates")] | |
public void ShouldSaveIfArticleValidates() | |
{ | |
/* Setup */ | |
MockRepository mocks = new MockRepository(); | |
IDataAccess<Article> dataAccess = mocks.StrictMock<IDataAccess<Article>>(); | |
IValidate<Article> validator = mocks.StrictMock<IValidate<Article>>(); | |
// SUT | |
ArticleRepository repository = new ArticleRepository(dataAccess, validator); | |
// Test data | |
Article article = new Article("My test article"); | |
With.Mocks(mocks).Expecting(delegate | |
{ | |
/* Record */ | |
Expect.Call(validator.Validate(article)).Return(true); | |
Expect.Call(() => dataAccess.Save(article)); | |
}) | |
.Verify(delegate | |
{ | |
/* Replay */ | |
repository.Save(article); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment