TypeMock LinQ DataContext in CSLA BO

Still not quiet clear how this work, but I finally made it, the basic idea is:

I made CSLA BO which calls LinQ DataContext to do the retrieve and update. The main purpose for this is to encapsulate LinQ synatx from CSLA coding.

To test CSLA BO without database, mock LinQ DataContext is necessary. So TypeMock is here, even I add IDataConext into CSLA BO, I found I can not pass this IDataconext as a constructor parameter to it. Fortunately, TypeMock can do kind of ‘internal object injection’ to mock the database!

[Test]
[VerifyMocks]
public void CanGetTraineeName()
{
Gain.DalLinq.Trainee t = new Gain.DalLinq.Trainee();
t.Given_Names = “Frank”;
SerializableEntity<Gain.DalLinq.Trainee> st = new SerializableEntity<DalLinq.Trainee>(t);

// This is the key to mock the future dataconext object.
Mock mocky = MockManager.Mock(typeof(TrainingDataContext));

using (RecordExpectations r = RecorderManager.StartRecording())
{
ITrainingDataContext mockedDbx = new TrainingDataContext();

// Mock GetTrainee method. parameter doesn’t matter.
SerializableEntity<Gain.DalLinq.Trainee> mocked = mockedDbx.GetTrainee(2);

r.Return(st);
}

Trainee item = Trainee.GetTrainee(2);
Assert.AreEqual(“Frank”, item.GivenNames);

[Test]
[VerifyMocks]
[ExpectedException(typeof(DataPortalException))]
public void CanCatchUpdateTraineeException()
{

Gain.DalLinq.Trainee t = new Gain.DalLinq.Trainee();
t.Given_Names = “Frank”;
SerializableEntity<Gain.DalLinq.Trainee> st = new SerializableEntity<DalLinq.Trainee>(t);

Mock mocky = MockManager.Mock(typeof(TrainingDataContext));

using (RecordExpectations r = RecorderManager.StartRecording())
{

ITrainingDataContext dtx = new TrainingDataContext();
SerializableEntity<Gain.DalLinq.Trainee> mocked = dtx.GetTrainee(1);

r.Return(st);

int m = dtx.SaveTrainee(null, null);

r.Return(-1122);
}

Trainee oldTrainee = Trainee.GetTrainee(2);
Assert.AreEqual(“Frank”, oldTrainee.GivenNames);

oldTrainee.GivenNames = “AAAA”;
oldTrainee.Save();

}

[Test]
[VerifyMocks]
public void CanUpdateTrainee()
{

Gain.DalLinq.Trainee t = new Gain.DalLinq.Trainee();
t.Given_Names = “Frank”;
SerializableEntity<Gain.DalLinq.Trainee> st = new SerializableEntity<DalLinq.Trainee>(t);

Mock mockDbx = MockManager.Mock(typeof(TrainingDataContext));

using (RecordExpectations r = RecorderManager.StartRecording())
{

TrainingDataContext dtx = new TrainingDataContext();
SerializableEntity<Gain.DalLinq.Trainee> mocked = dtx.GetTrainee(1);

r.Return(st);

int m = dtx.SaveTrainee(null, null);

r.Return(0);
}

Trainee oldTrainee = Trainee.GetTrainee(2);
Assert.AreEqual(“Frank”, oldTrainee.GivenNames);

oldTrainee.GivenNames = “AAAA”;
Trainee newTrainee = oldTrainee.Save();
Assert.AreEqual(“AAAA”, newTrainee.GivenNames);

}

The idea is from Ben Hall. I add interface in, to focus on testing CSLA only.

Even it’s a great solution to separate dbtest, but I think if we use Windsor Container correctly, those mock tests can be much simpler and clearer, I will post this later.

Advertisements

2 thoughts on “TypeMock LinQ DataContext in CSLA BO

  1. Dear Frank,
    thank you for using Typemock Isolator, and I’d like to point our users in this direction if they come across a similar problem. Please feel free to contact me directly at avik at typemock dot com if you need any technical assistance or have any questions what so ever.
    Sincerely,
    Avi Kaye
    Typemock

  2. Pingback: csla

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s