Skip to content

Instantly share code, notes, and snippets.

@colinbowern
Last active December 17, 2015 04:39
Show Gist options
  • Save colinbowern/5552127 to your computer and use it in GitHub Desktop.
Save colinbowern/5552127 to your computer and use it in GitHub Desktop.
Add functionality to verify mapping similar to FluentNHibernate's PersistenceSpecification class.
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Transactions;
using FluentAssertions;
// ReSharper disable CheckNamespace
namespace System.Data.Linq
// ReSharper restore CheckNamespace
{
public static class DataContextExtensions
{
public static void VerifyTheMapping<T>(this DataContext dataContext, T first, Expression<Func<T, bool>> identity) where T : class
{
using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew))
{
var table = dataContext.GetTable<T>();
table.InsertOnSubmit(first);
dataContext.SubmitChanges();
dataContext.Refresh(RefreshMode.OverwriteCurrentValues, first);
var second = table.First(identity);
second.ShouldHave().AllProperties().EqualTo(first);
transaction.Dispose();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using FizzWare.NBuilder;
using MyApp.Domain;
using Xunit;
namespace MyApp.IntegrationTests.Persistence
{
public class WidgetFacts : IUseFixture<DatabaseFixture>
{
public void SetFixture(DatabaseFixture data) { }
[Fact]
public void CanPersistWidget()
{
using (var dataContext = new AppDataContext(DatabaseFixture.ConnectionString))
{
var entity = Builder<Widget>.CreateNew().Build();
dataContext.VerifyTheMapping(entity, widget => widget.Id == entity.Id);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment