Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8004181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:50:56+00:00 2026-06-04T16:50:56+00:00

I am using PetaPoco on my current project as a micro ORM and i

  • 0

I am using PetaPoco on my current project as a micro ORM and i must say that i like it. However, i found myself struggling with simple scenario – unit testing services that use 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);
    }
}

I am using IoC (Castle.Windsor) for injection of both IMyService and PetaPoco.Database wherever needed.

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();   
    }

}

I am aware that this can be solved if i extract an Interface from PetaPoco.Database and do the mocking against it, or by virtualizing PetaPoco’s methods that i want to mock, but the point is that i don’t want to make changes to PetaPoco at all.

Is this doable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:50:57+00:00Added an answer on June 4, 2026 at 4:50 pm

    My branch located here: https://github.com/schotime/PetaPoco already has an interface defined for the Database class.

    Also there is my new Fork https://github.com/schotime/NPoco or NPoco on nuget which has the same api.

    I would use one of these. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using PetaPoco Micro-ORM with my ASP.NET 4.0 project. PetaPoco T4 template (Database.tt)
I am using PetaPoco Micro-ORM with C# 4.0. The code below retrieves a single
I need to create a DAL and repositories using petapoco. The difficulty that comes
I'm using PETAPOCO make a list of generic objects that are then bound to
I like the micro approach of Dapper, Massive, PetaPoco etc. and I like to
I'm using the PetaPoco mini-ORM, which in my implementation runs stored procedures and maps
I'm trying to setup an output parameter using PetaPoco. I found someone using this
I am new to PetaPoco ORM in .net and i found it quite confused
I'm using a homebrewed repository pattern (!) together with PetaPoco in my latest project.
Just started a 'real world' project using .NET MVC, Ninject, PetaPoco and plan to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.