using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using Rhino.Mocks.Constraints;
using Rhino.Mocks.Impl;
using Rhino.Mocks.Interfaces;
namespace LogAnalyzer
{
[TestFixture]
public class LogAnalyzerTests
{
[Test]
public void Analyze()
{
MockRepository mocks = new MockRepository();
IWebService simulatedService =
//mocks.StrictMock();
mocks.DynamicMock();
using( mocks.Record() )
{
simulatedService.LogError("too short:abc.txt");
}
LogAnalyzer log = new LogAnalyzer(simulatedService);
log.Analyze("abc.txt");
//mocks.Verify(simulatedService);
mocks.VerifyAll();
}
[Test]
public void ReturnResultsFromMock()
{
MockRepository mocks = new MockRepository();
IGetResults resultGetter = mocks.DynamicMock();
using( mocks.Record() )
{
resultGetter.GetSomeNumber("a");
LastCall.Return(1);
resultGetter.GetSomeNumber("b");
LastCall.Return(2);
resultGetter.GetSomeNumber("c");
LastCall.Return(3);
}
int result = resultGetter.GetSomeNumber("b");
Assert.AreEqual(2, result);
int result2 = resultGetter.GetSomeNumber("a");
Assert.AreEqual(1, result2);
int result3 = resultGetter.GetSomeNumber("c");
Assert.AreEqual(3, result3);
mocks.VerifyAll();
}
[Test]
public void ReturnResultsFromStub()
{
MockRepository mocks = new MockRepository();
IGetResults result = mocks.Stub();
using( mocks.Record() )
{
result.GetSomeNumber("a");
LastCall.Return(1);
}
int res = result.GetSomeNumber("a");
Assert.AreEqual(1, res);
}
[Test]
public void StubSimulatingException()
{
MockRepository mocks = new MockRepository();
IGetResults result = mocks.Stub();
using( mocks.Record() )
{
result.GetSomeNumber("A");
LastCall.Throw(new OutOfMemoryException("not enough"));
}
//result.GetSomeNumber("A");
}
[Test]
public void Analyze_WebServiceThrows_SendEmail()
{
MockRepository mocks = new MockRepository();
IWebService stubService = mocks.Stub();
IEmailService mockEmail = mocks.DynamicMock();
using( mocks.Record() )
{
stubService.LogError("whatever");
LastCall.Constraints(Rhino.Mocks.Constraints.Is.Anything());
LastCall.Throw(new Exception("fake exception"));
mockEmail.SendEmail("a", "subject", "fake exception");
}
LogAnalyzer log = new LogAnalyzer();
log.Service = stubService;
log.Email = mockEmail;
string tooShortFileName = "abc.txt";
log.Analyze(tooShortFileName);
mocks.VerifyAll();
}
}
public interface IGetResults
{
int GetSomeNumber(string str);
}
public class ManualMockService : IWebService
{
public string LastError;
public void LogError(string message)
{
LastError = message;
}
}
public interface IEmailService
{
void SendEmail(string to, string subject, string message);
}
public class LogAnalyzer
{
private IWebService service;
private IEmailService email;
public LogAnalyzer(IWebService service)
{
this.service = service;
}
public LogAnalyzer() { }
public IWebService Service
{
get { return service; }
set { service = value; }
}
public IEmailService Email
{
get { return email; }
set { email = value; }
}
public void Analyze(string fileName)
{
if( fileName.Length < 8 )
{
try
{
service.LogError("too short:" + fileName);
}
catch( Exception e )
{
email.SendEmail("a", "subject", e.Message);
}
}
}
}
public interface IWebService
{
void LogError(string message);
}
class Program
{
static void Main(string[] args)
{
}
}
}
the art of unit testing에서 적당히 짬뽕....;;;
분류없음2009/10/09 01:44