1: // Klasa pomocnicza do mockowania
2: public abstract class MockedTestCase
3: {
4: private MockRepository mockery;
5:
6: protected MockRepository Mockery
7: {
8: get { return this.mockery; }
9: }
10:
11: public virtual void SetUp()
12: {
13: this.mockery = new MockRepository();
14: }
15: }
16:
17: // Bazowa klasa do testowania logiki biznesowej
18: public class ServiceTestCase<TService> : MockedTestCase where TService : IBaseService
19: {
20: protected Func<TService> sutCreator;
21:
22: private TService sut;
23:
24: protected TService Sut
25: {
26: get { return this.sut; }
27: set { this.sut = value; }
28: }
29:
30: public static IUserSession GetStubbedUserSession()
31: {
32: IUserSession session = new UserSessionStub();
33: User sampleUser = new User();
34: sampleUser.LoginName = "test";
35: session.CurrentUser = sampleUser;
36:
37: return session;
38: }
39:
40: [SetUp]
41: public override void SetUp()
42: {
43: base.SetUp();
44: if (this.sutCreator != null)
45: {
46: this.Sut = this.sutCreator();
47: }
48: }
49:
50: public class UserSessionStub : BaseUserSession
51: {
52: }
53: }
54:
55: // Nasza klasa testujaca
56: [TestFixture]
57: public class DefaultTelesalesCompanyInfoApplicationImportProcessingServiceTests : ServiceTestCase<DefaultTelesalesCompanyInfoApplicationImportProcessingService>
58: {
59: public DefaultTelesalesCompanyInfoApplicationImportProcessingServiceTests()
60: {
61: this.sutCreator = delegate { return Mockery.PartialMock<DefaultTelesalesCompanyInfoApplicationImportProcessingService>(); };
62: }
63:
64: [Test]
65: public void Process_GetApplicationsFromDatabaseForProcessingReturnsNull_ThrowsCriticalBusinessLogicException()
66: {
67: using (Mockery.Record())
68: {
69: Expect
70: .Call(this.Sut.GetApplicationsFromDatabaseForProcessing())
71: .Return(null);
72: }
73:
74: using (Mockery.Playback())
75: {
76: Assert.Throws<CriticalBusinessLogicException>(delegate { this.Sut.Process(); });
77: }
78: }
79: ...