NH ProfA testing story
Remember that I mentioned the difference about working and production quality?
One of the things that separate the two that in production quality software, you don't need to know which buttons not to push. Here is a simple example. For a while now, if you tried to bring up two instances of NH Prof, the second one would crash. That wasn't something that you really want to show the users. Today I got back to doing NH Prof stuff, getting it ready for public beta, and I decided that the first thing to do was to tackle this easy feature.
Doing this is actually not an issue, testing this, however, was a problem. I have two application instances and four layers to go through. Here is the first test:
[Test]
public void When_creating_two_instances_of_application_will_tell_the_other_to_pop_up()
{
var bus = MockRepository.GenerateStub<IBus>();
var listener = new NHibernateAppenderRemotingLoggingEventListener(bus);
listener.Start();
try { var anotherBus = MockRepository.GenerateStub<IBus>(); var anotherListener = new NHibernateAppenderRemotingLoggingEventListener(anotherBus);
anotherListener.Start();
Assert.Fail("Exception was expected");
}
catch (AnotherApplicationIsAlreadyRunningAndControlWasMovedToItException)
{
}
bus.AssertWasCalled(x => x.Publish(null),
options => options.Constraints(Is.TypeOf<BringApplicationToFront>())
);
}
The logging event listener is the reason that we can't bring up two instances of this. We use this to listen to running applications, and having several of the profilers running at one time isn't going to be helpful. And just to deal with the nitpickers, the four layers I mentioned are: Communication layer, backend, front end, actual user interface.
Here is an example that tests communication between the back end and the front end:
[Test]
public void When_bring_up_to_front_message_is_publish_observer_should_raise_event_for_UI()
{
var facility = new BusFacility();
var observer = new ModelBuilderObserver(action => action());
bool wasBroughtToFront = false;
observer.ShouldBringApplicationToFront += () => wasBroughtToFront = true;
facility.Initialize(observer);
using(facility.Start())
{
facility.Bus.Publish(new BringApplicationToFront());
facility.Bus.WaitForAllMessages();
}
Assert.IsTrue(wasBroughtToFront);
}
As for the actual UI code, I am not sure how to test that. I did a manual test, but I am not sure that I am happy about this. Then again, we are talking about testing this line of code:
observer.ShouldBringApplicationToFront += () => Activate();
I can just test that the event was subscribed to, but I don't really see this as valuable.
More posts in "NH Prof" series:
- (09 Dec 2010) Alert on bad ‘like’ query
- (10 Dec 2009) Filter static files
- (16 Nov 2009) Exporting Reports
- (08 Oct 2009) NHibernate Search Integration
- (19 Aug 2009) Multiple Session Factory Support
- (07 Aug 2009) Diffing Sessions
- (06 Aug 2009) Capturing DDL
- (05 Aug 2009) Detect Cross Thread Session Usage
- (22 May 2009) Detecting 2nd cache collection loads
- (15 May 2009) Error Detection
- (12 May 2009) Queries by Url
- (04 Feb 2009) View Query Results
- (18 Jan 2009) Superfluous <many-to-one> update
- (18 Jan 2009) URL tracking
- (10 Jan 2009) Detecting distributed transactions (System.Transactions)
- (06 Jan 2009) The Query Cache
- (05 Jan 2009) Query Duration
- (24 Dec 2008) Unbounded result sets
- (24 Dec 2008) Row Counts
Comments
Comment preview