Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • roszman 1 post 71 karma points
    Jan 18, 2016 @ 14:10
    roszman
    0

    Testing custom repository

    First post so hi all :)

    can anyone tell my how can i test my custom repositories? i have a repo which is using umbraco database:

    public class MyRepository : IMyRepository { protected readonly UmbracoDatabase db;

        public MyRepository()
        {
            db = ApplicationContext.Current.DatabaseContext.Database;
        }
    
        public void EditRestaurant(Restaurant restaurant)
        {
            var updatedRestaurant = db.SingleOrDefault<RestaurantPoco>("SELECT * FROM Restaurants WHERE Id = @0", restaurant.ID);
            if (updatedRestaurant == null)
            {
                throw new KeyNotFoundException("Restaurant has not been found.");
            }
    
            db.Update("Restaurants", "Id", Mapper.Map<Restaurant, RestaurantPoco>(restaurant, updatedRestaurant));
        }
    }
    

    i would like to use test database for my unit tests.

    How can i do this?

    thx in advance for help

  • Carlos Casalicchio 176 posts 736 karma points
    Mar 10, 2020 @ 15:36
    Carlos Casalicchio
    0

    I'm surprised no one has ever answered this.

    In order to make it work with your Unit Tests, add a app.config to your project (Unit Test Project), then copy the connection string from the web.config into the app.config.

    Then, in your test class, use this structure (or similar).

    namespace yourNamespace
    {
        using yourproject.Core.Services;
        using Microsoft.VisualStudio.TestTools.UnitTesting;
    
        using Umbraco.Core.Persistence;
    
        [TestClass]
        public class MyRepository_Tests
        {
            private Database db;
            private MyRepository repository;
    
            [TestInitialize]
            public void Initialize()
            {
                db = new Database("umbracoDbDSN"); //this is the connection string from the app.config
                repository = new MyRepository(db);
            }
    
            [TestMethod]
            public void MyRepository_EditRestaurant()
            {
                //Arrange
                var id = 10;
                Restaurant restaurant = service.GetRestaurant(id);
                      //.... your edit code here 
    
                //Act
                var edited = service.EditRestaurant(restaurant);
    
                //Assert
                Assert.IsNotNull(edited);
            }
        }
    }
    
  • Carlos Casalicchio 176 posts 736 karma points
    Mar 10, 2020 @ 16:13
    Carlos Casalicchio
    0

    And, in case you need the Application Context, use Moq and add this

            db = new Database("umbracoDbDSN");
            var umbracoSettings = Mock.Of<IUmbracoSettingsSection>();
            var eventsMsgFactory = Mock.Of<IEventMessagesFactory>();
            var logger = Mock.Of<ILogger>();
            var dbFactory = Mock.Of<IDatabaseFactory2>();
            var sqlSyntax = Mock.Of<ISqlSyntaxProvider>();
            var dbProvider = "System.Data.SqlClient";
            var Uof = Mock.Of<IScopeUnitOfWorkProvider>();
            var profiler = Mock.Of<IProfiler>();
            var repositoryFactory = new RepositoryFactory(new CacheHelper(), logger, sqlSyntax, umbracoSettings);
    
            var serviceContext = new ServiceContext(repositoryFactory, Uof, new CacheHelper(), logger, eventsMsgFactory);
    
            var appContext = new ApplicationContext(new DatabaseContext(dbFactory, logger, sqlSyntax, dbProvider), serviceContext, new CacheHelper(), new ProfilingLogger(logger, profiler));
            service = new GeoLocationService(db, appContext.DatabaseContext);
    
Please Sign in or register to post replies

Write your reply to:

Draft