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.
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);
}
}
}
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);
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;
i would like to use test database for my unit tests.
How can i do this?
thx in advance for help
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).
And, in case you need the Application Context, use Moq and add this
is working on a reply...