English 中文(简体)
如何使用 PetaPoco.Database 的单位测试服务
原标题:How to unit test service that is using PetaPoco.Database

我在使用",http://www.topten software.com/petapoco/">PetaPoco ,关于我目前作为微微ORM的工程,我必须说我喜欢。然而,我发现自己在苦苦挣扎着简单的假想-使用PetaPoco的单位测试服务。Database

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

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

这是可行的吗?

最佳回答

我的分支位于此 : https://github.com/schotime/PetaPoco 已经为数据库类定义了一个接口 。

还有我的新福克 < a href="https://github.com/schotime/NPoco" rel="nofollow" >https://github.com/schotime/NPoco 或NPoco关于核桃的NPoco,它们都有相同的。

我将使用其中之一。 ))

问题回答

您已经在与 PetaPoco 进行抽象互动。 数据库使用 IMy Services 互动, 所以为什么您需要另外的抽象? 您目前的做法应该能够测试与数据库的交互, 例如 IMyServic 。

public class AuthenticationService 
{
    private IMyService myService;

    public AuthenticationService(IMyService service) 
    {
        ...
    }

    public void Authenticate(string username, string password)
    {
       var user = myService.GetUser(username); // <-- Hits the database
    }
}

并测试它,你只是模拟互动 使用一个模拟/弹簧的IMYServices。

现在,关于你最初的解决方案,如果PetaPoco公共方法不是虚拟的,那么我会用叉子来修改代码,并给他们发送拉动请求。 否则,你的方法在我看来就很好了。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签