我在使用",http://www.topten software.com/petapoco/">PetaPoco ,关于我目前作为微微ORM的工程,我必须说我喜欢。然而,我发现自己在苦苦挣扎着简单的假想-使用
public class MyService : IMyService
{
private readonly PetaPoco.Database _database;
public MyService(PetaPoco.Database database)
{
_database = database;
}
public void SaveSomething(MyObject myObject)
{
//...custom logic
_database.Save(myObject);
}
}
我使用 " 坚固 " IoC 强 " (Castle. Windsor)注射 " 坚固的 " IMY Service 强 " 和 " 坚固的 " PetaPoco.Database 强 " 。
Now, when i try to unit test my service i am unable to properly mock stub PetaPoco.Database in order to verify that the Save method was properly invoked. I am using NUnit and Rhino.Mocks for unit testing and mocking.
[TestFixture]
public class MyServiceTests
{
private PetaPoco.Database _database;
[SetUp]
public void SetUp()
{
_database = MockRepository.GenerateMock<Database>("");
}
[Test]
public void ShouldProperlySaveSomething()
{
//Arrange
var myObject = new MyObject();
_database.Expect(db => db.Save(Arg<MyObject>.Is.Anything));
var myService = new MyService(_database);
//Act
myService.SaveSomething(myObject);
//Assert
_database.VerifyAllExpectations();
}
}
我知道,如果我从“强”PetaPoco.Database 强”中提取一个接口,并嘲笑它,或者将我想嘲弄的PetaPoco方法虚拟化,就可以解决这个问题,但重点是我不想对“强”PetaPoco 强”作任何修改。
这是可行的吗?