Welcome to MSDN Blogs Sign in | Join | Help

An Overview of Unit Testing Duplex WCF Services and Clients

In the last couple of posts, I've demonstrated how to isolate implementation from WCF contract definition and behavior in a duplex communication scenario. These posts have been rather detailed, so it occurred to me that you might benefit from an overview.

The main goal was to ensure testability of implementations of both service and client. While the contracts reference WCF (System.ServiceModel), the implementations themselves only reference the contracts, and not WCF.

DependencyGraph

This diagram illustrates dependencies between the various libraries. The left side contains server-side components, while the right side contains client-side components. The only shared components are WCF and the Contract library.

Note that neither StuffServer nor StuffAgent has any dependency on System.ServiceModel, although they have a dependency on Contract. The same is true for both unit test projects.

The top boxes on both sides (MyServer and MyClient) are hypothetical top-level executables such as I've discussed before, although never shown any code from. These Humble Executables tie everything together by referencing all relevant libraries, but if you prefer, you can just as well use a configurable Dependency Injection container - Oran Dennison explains how to do this on Spring.NET, while Ayende has a Windsor ServiceHostFactory.

Here are the main posts on the subject:

If you would like to take a closer look at my sample code, I've attached it to this post. As usual, the standard disclaimers apply.

Unit Testing Duplex WCF Clients

In my previous post, I explained how to unit test a WCF service with callbacks. Since the scenario involves duplex communication, the service is also a client, and vice versa, so it's only reasonable to look at the 'client' as well, to examine how to best approach unit testing such a beast.

Since both ends both act as sender and receiver, they are each both client and service. None the less, there's still a clear distinction, since the client is the party that initiates and terminates a session, whereas the service is always ready and available for whatever client that may come along.

Ensuring proper isolation and testability is most difficult for the service, which was why I decided to tackle that issue first. Making the client testable is much easier, and I'll describe how to do that here by continuing the example from my former post.

When writing the client, the only thing I really need to abstract away is the creation of the WCF proxy and its callback instance.

When you look at the standard WCF documentation for duplex clients, a duplex proxy can be created by a DuplexChannelFactory<TChannel> like this:

DuplexChannelFactory<IStuffService> cf = 
    new DuplexChannelFactory<IStuffService>(callbackService,
        binding, remoteAddress);

You can then invoke CreateChannel on the cf instance to get an instance of an IStuffService proxy.

However, now that you have decided to implement your client without any reference to WCF, the use of DuplexChannelFactory<TChannel> and its ilk from within that library is not permitted (and should indeed be impossible, since you don't have a reference to System.ServiceModel).

What the client really needs is something that can create an instance of an IStuffService proxy, while itself receiving an instance of IStuffCallbackService (which is the type of the callbackService parameter above). In other words, I'll need to define an abstraction that takes IStuffCallbackService as input and returns IStuffService.

In a move very similar to the approach described previously, a Func<IStuffCallbackService, IStuffService> is just what we need. Hence, a rudimentary implementation of StuffClient would be similar to this:

public class StuffClient : IStuffCallbackService
{
    private readonly Func<IStuffCallbackService, IStuffService> createProxy_;
 
    public StuffClient(Func<IStuffCallbackService, IStuffService> proxyCreator)
    {
        if (proxyCreator == null)
        {
            throw new ArgumentNullException("proxyCreator");
        }
 
        this.createProxy_ = proxyCreator;
    }
 
    public void DoStuff(string stuff)
    {
        IStuffService proxy = this.createProxy_(this);
        proxy.DoStuff(stuff);
 
        // Remeber to safely dispose of proxy
    }
 
    // Other members, including IStuffCallbackService Members...
}

The abstract proxy creator is required by way of Constructor Injection, and is used in the DoStuff method to create an IStuffService proxy instance, which may or may not be a WCF proxy.

If the proxy instance is a real WCF proxy, it will implement IDisposable and should be properly closed. There are several strategies for dealing with this situation, but to keep the example as simple as possible, I've chosen to omit this particular aspect, and leave it as an exercise for the interested reader :)

Although I have yet to show the actual implementation, note that StuffClient implements IStuffCallbackService, so it can return itself to the createProxy_ delegate instance as the callback service. Obviously, if you use the same object as both sender and receiver, you need to do so in a thread-safe manner, but since I can, in this particular context, bask in the luxury of writing sample code, I choose to ignore thread-safety completely :)

#region IStuffCallbackService Members
 
public void StuffWasDone(string result)
{
    this.stuffResults_.Add(result);
}
 
#endregion

where stuffResults_ is a List<string>.

Unit testing StuffClient is now fairly simple. We'll need an implementation of IStuffService to act as a Test Double, so I've created a simple SpyStuffService class that records all the stuff strings in a List<string> called Stuffs.

First of all, we need to ensure that the proxyCreator delegate instance receives a proper instance of IStuffCallbackService as input:

[TestMethod]
public void DoStuffWillCreateProxyByPassingNonNullCallback()
{
    // Fixture setup
    string anonymousStuff = "ploeh";
    SpyStuffService dummy = new SpyStuffService();
    StuffClient sut = new StuffClient(callbackService =>
        {
            Assert.IsNotNull(callbackService,
                "Callback service");
            return dummy;
        });
    // Exercise system
    sut.DoStuff(anonymousStuff);
    // Verify outcome (done by inline mock);
    // Teardown
}

A bit unusually, test verification happens inside the delegate defined by the lambda expression passed to the constructor of StuffClient - a technique I call an Inline Mock. Since C# is strongly typed, I can be assured that if callbackService is not null, it will be an instance of IStuffCallbackService.

Next, we may want to verify that when DoStuff is invoked, data will be transferred to the service:

[TestMethod]
public void DoStuffWillSendStuffToService()
{
    // Fixture setup
    string expectedStuff = "ploeh";
    SpyStuffService spy = new SpyStuffService();
    StuffClient sut =
        new StuffClient(callbackService => spy);
    // Exercise system
    sut.DoStuff(expectedStuff);
    // Verify outcome
    Assert.AreEqual<string>(expectedStuff, 
        spy.Stuffs.First(), "Stuff");
    // Teardown
}

The Spy is passed to the SUT in the constructor, and later inspected in the verification phase. Pretty classic four-phase unit test setup...

Finally, we may want to test the SUT's implementation of IStuffCallbackService:

[TestMethod]
public void StuffWasDoneWillAddResultToStuffResultList()
{
    // Fixture setup
    string expectedStuff = "ploeh";
    SpyStuffService dummy = new SpyStuffService();
    StuffClient sut = 
        new StuffClient(callbackService => dummy);
    // Exercise system
    sut.StuffWasDone(expectedStuff);
    // Verify outcome
    Assert.AreEqual<string>(expectedStuff,
        sut.StuffResults.First(), "Stuff result");
    // Teardown
}

At this point, the SpyStuffService instance is not used by the SUT, but we need it for the Func<IStuffCallbackService, IStuffService> instance used in the StuffClient constructor, so it's a classic example of a Test Dummy.

The test invokes the StuffWasDone method and subsequently verifies that the string passed to it becomes available in the StuffResults list, so again, it's really just a standard four-phase unit test.

If you've managed to read this far, I owe it to you to show how to set up StuffClient with a real WCF proxy. Remember, this must be done in another library that can reference System.ServiceModel, since the library where StuffClient is implemented must not reference WCF. The most natural place to do this is in a Humble Object, such as a top-level executable, as described here.

StuffClient client = new StuffClient(callbackService =>
    {
        DuplexChannelFactory<IStuffService> cf = 
            new DuplexChannelFactory<IStuffService>(callbackService,
                binding, remoteAddress);
        return cf.CreateChannel();
    });

Note that this is the place where WCF reappears after its long absence. The StuffClient implementation has no dependency on WCF, and testability is ensured.

Update (2008-07-12): I've just posted an overview of the solution, as well as all the sample code.

Unit Testing Duplex WCF Services

One of my readers recently asked me about unit testing WCF services when they have callbacks. Given that I strongly believe that you should attempt to implement your services without referencing WCF at all, but duplex WCF services require you to get the callback instance from OperationContext.Current, how can these two forces be reconciled?

Fortunately, it's really not that hard. All you have to do is to replace the call to OperationContext.GetCallbackChannel<T> with something abstract. On .NET 3.5, the easiest abstraction is Func<TResult>, which has the same signature, but if you are on .NET 3.0, you can always define a similar delegate type of your own.

Let's say that your contracts look like this:

[ServiceContract(CallbackContract = typeof(IStuffCallbackService))]
public interface IStuffService
{
    [OperationContract]
    void DoStuff(string stuff);
}
 
public interface IStuffCallbackService
{
    [OperationContract]
    void StuffWasDone(string result);
}

Since it would ruin testability of the IStuffService implementation if it was to use OperationContext.GetCallbackChannel<T> to create a new instance of IStuffCallbackService, it needs an instance of Func<IStuffCallbackService> instead. As I favor Constructor Injection, the complete implementation looks like this:

public class StuffService : IStuffService
{
    private readonly Func<IStuffCallbackService> createCallbackChannel_;
 
    public StuffService(Func<IStuffCallbackService> callbackCreator)
    {
        if (callbackCreator == null)
        {
            throw new ArgumentNullException("callbackCreator");
        }
        this.createCallbackChannel_ = callbackCreator;
    }
 
    #region IStuffService Members
 
    public void DoStuff(string stuff)
    {
        // Implementation goes here...
        string stuffResult =
            new string(stuff.ToCharArray().Reverse().ToArray());
 
        this.createCallbackChannel_().StuffWasDone(stuffResult);
    }
 
    #endregion
}

Such an implementation is imminently testable, as this test demonstrates:

[TestMethod]
public void DoStuffWillInvokeCallbackService()
{
    // Fixture setup
    string anonymousStuff = "ploeh";
    string expectedResult = 
        new string(anonymousStuff.ToCharArray().Reverse().ToArray());
 
    SpyStuffCallbackService spy = new SpyStuffCallbackService();
 
    StuffService sut = new StuffService(() => spy);
    // Exercise system
    sut.DoStuff(anonymousStuff);
    // Verify outcome
    Assert.AreEqual<string>(expectedResult,
        spy.StuffResults.First(), "Callback result");
    // Teardown
}

The SpyStuffCallbackService class is a simple Test Spy that records all the callbacks in the StuffResults collection.

When you let WCF host the service, you need to tell WCF to use OperationContext.GetCallbackChannel<IStuffCallbackService> as the delegate instance to the StuffService constructor. In my previous post, I demonstrated how to do that (that's what StuffInstancingBehavior.GetInstance does).

Update (2008-07-12): I've just posted an overvview of the solution, as well as all the sample code.

Posted by ploeh | 3 Comments
Filed under: ,

Modifying Behavior of WCF-Free Service Implementations

In my previous post, I explained how to implement a WCF service without referencing WCF. In simple cases, it works as described, but you may soon find yourself in a situation where you need to modify the behavior of the service when it's hosted by WCF.

Perhaps you need to control the service's ConcurrencyMode, or perhaps you need to set UseSynchronizationContext. These options are typically controlled by the ServiceBehaviorAttribute.

You may also want to provide an IInstanceProvider via a custom attribute that implements IContractBehavior.

However, you can't set these attributes on the service implementation itself, since it mustn't have a reference to System.ServiceModel.

In the rest of this post, I'll provide an example of how to achieve such results while still keeping WCF out of the service implementation.

Consider this service implementation:

public class StuffService : IStuffService
{
    private readonly Func<IStuffCallbackService> createCallbackChannel_;
 
    public StuffService(Func<IStuffCallbackService> callbackCreator)
    {
        if (callbackCreator == null)
        {
            throw new ArgumentNullException("callbackCreator");
        }
        this.createCallbackChannel_ = callbackCreator;
    }
 
    // IStuffService members here...
}

There are many cases (Dependency Injection is a common reason) where a a non-default constructor is desirable, so if you are wondering about this weird-looking Func<IStuffCallbackService> parameter, just consider that it might as well have been some other type of dependency. In a later post (edit 2008.06.29: available here), it will become apparent just why the constructor takes this specific input - for now, the important point is that the service implementation only has one, non-default constructor, and that it will throw if passed a null value.

Since WCF by default manages a service instance's lifetime, and the default behavior is to create a new instance per request, WCF requires the service to have a default constructor; that is, unless an IInstanceProvider is provided.

IInstanceProvider is defined in System.ServiceModel, so I can't implement this interface directly in the service library; instead, I create a new library that defines the WCF hosting behavior for the service. This library references both System.ServiceModel and the service library (the library that contains StuffService), so essentially, it's the library that sits on top of the dependency hierarchy and wires everything together.

In this library, I can implement IInstanceProvider via an IContractBehavior to provide new instances of StuffService:

public class StuffInstancingBehavior :
    IContractBehavior, IInstanceProvider
{
    #region IInstanceProvider Members
 
    public object GetInstance(InstanceContext instanceContext,
        Message message)
    {
        return this.GetInstance(instanceContext);
    }
 
    public object GetInstance(InstanceContext instanceContext)
    {
        return new StuffService(() =>
            OperationContext.Current.
            GetCallbackChannel<IStuffCallbackService>());
    }
 
    public void ReleaseInstance(InstanceContext instanceContext,
        object instance) { }
 
    #endregion
 
    #region IContractBehavior Members
 
    public void AddBindingParameters(ContractDescription description,
        ServiceEndpoint endpoint,
        BindingParameterCollection bindingParameters) { }
 
    public void ApplyClientBehavior(ContractDescription description,
        ServiceEndpoint endpoint, ClientRuntime clientRuntime) { }
 
    public void ApplyDispatchBehavior(ContractDescription description,
        ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
        dispatchRuntime.InstanceProvider = this;
    }
 
    public void Validate(ContractDescription description,
        ServiceEndpoint endpoint) { }
 
    #endregion
}

There are really only two lines of code of interest in this code listing: ApplyDispatchBehavior associates this IInstanceProvider implementation with the DispatchRuntime, and the GetInstance method creates the StuffService instance itself.

Notice that while we will not accept the use of OperationContext.Current in the service library, it's perfectly legal to use it here.

Applying an IEndpointBehavior can be done either via configuration or by programmatically adding it to the ServiceHost before opening it.

Since WCF will not be able to create an instance of StuffService without StuffInstancingBehavior, it's not really optional, so making it configurable doesn't make a whole lot of sense. This means that I should add it programmatically.

To support hosting in as wide a range of scenarios as possible, the best way to ensure that the ServiceHost is always created correctly (even when hosted in IIS) is by implementing a dedicated ServiceHostFactory (let's call it StuffServiceHostFactory). The CreateServiceHost method can be implemented as simply as this:

return new StuffServiceHost(baseAddresses);

Obviously, the real behavior configuration takes place in the StuffServiceHost class, which derives from ServiceHost. The entire implementation takes place in the constructor:

public StuffServiceHost(params Uri[] baseAddresses)
    : base(typeof(StuffService), baseAddresses)
{
    foreach (ContractDescription cd in
        this.ImplementedContracts.Values)
    {
        cd.Behaviors.Add(new StuffInstancingBehavior());
    }
 
    this.Description.Behaviors.Find<ServiceBehaviorAttribute>().
        ConcurrencyMode = ConcurrencyMode.Reentrant;
}

It adds StuffInstancingBehavior to all contracts, which will cause WCF to use its implementation of IInstanceProvider each time it wants to create a new instance of the StuffService. It also modifies the service's ConcurrencyMode; again, why it does that will become apparent in a future post, but I just wanted to show how you can programmatically achieve the same result as decorating your service with the ServiceBehaviorAttribute, since you can't do that directly on StuffService because it's being implemented in a library that doesn't reference WCF.

If you want to host StuffService in IIS, you just need to specify StuffServiceHostFactory in the .svc file's Factory attribute; if you want to host StuffService in your own process, you can just create a new instance of StuffServiceHost directly (or use an instance of StuffServiceHostFactory to create it).

All this behavior modification is taking place without StuffService being aware of it at all, and you can concentrate on implementing the service in whatever way you prefer, while keeping it totally testable and WCF runtime services far away from it. That's a very clean separation of concerns in my book.

Update (2008-07-12): Sample code is available here.

Posted by ploeh | 2 Comments

Implementing WCF Services Without Referencing WCF

More than a year ago, I wrote my first post on unit testing WCF services. One of my points back then was that you have to be careful that the service implementation doesn't use any of the services provided by the WCF runtime environment (if you want to keep the service testable). As soon as you invoke something like OperationContext.Current, your code is not going to work in a unit testing scenario, but only when hosted by WCF.

This point is as true today as it was one and a half year ago.

So how do you ensure that you don't accidentally invoke WCF runtime services in the service code?

By being very, very careful..?

Unless you are very disciplined and work alone on the service, that's probably not a very good solution. While you may be skillful enough to pull it off, Joe Junior Programmer is going to come along during your one week of vacation and mess it all up.

Fortunately, a more robust solution exists: You can actually implement a WCF service without referencing WCF at all. How about that?

Obviously, when declaring the service contract, you will need to reference System.ServiceModel to put the right attributes on the contract:

[ServiceContract]
public interface IStuffService
{
    [OperationContract]
    void DoStuff(string stuff);
}

The contract library contains only the contract, but no implementation. The concrete implementation can be placed in a separate library that doesn't reference System.ServiceModel. As long as you have a reference to the contract library, you can still implement the service:

public class StuffService : IStuffService
{ 
    #region IStuffService Members
 
    public void DoStuff(string stuff)
    {
        // Implementation goes here...    }
 
    #endregion
}

Since the entire WCF part of the contract is specified using attributes, you don't need a reference to System.ServiceModel in the implementation library even though the contract library requires this reference.

Now you can implement the service without fear of accidentally adding WCF dependencies, since you don't have a reference to WCF at all.

To prevent other developers from accidentally adding a reference to System.ServiceModel while you are away, you can actively enforce that convention, as exemplified in Glenn's post.

In very simple cases, this is all you need to do, but I'll admit that this example is a bit naive; as soon as you need to apply behaviors or other WCF-specific modifications to the service, it becomes more complex. In many cases, this is done by applying attributes to the service implementation (such as ServiceBehaviorAttribute), but this is not possible when you don't have a reference to System.ServiceModel.

In a future post, I'll explain how to get around some of these issues. Update: This post is now available here.

Posted by ploeh | 2 Comments
Filed under: ,

Speaking in ANUG

Aarhus .NET User Group has been so kind to invite me to come and give a session on June 25th, 2008, and I've elected to speak about TDD and Installers, a subject that regular readers of this blog would correctly surmise is dear to my heart.

Read more about the event here (in Danish, as will be the session itself).

If you are in Århus that day, I hope you will consider coming by and saying hello as well.

Posted by ploeh | 10 Comments
Filed under:

Test-Specific APIs With Extension Methods

Writing maintainable tests that clearly communicate intent can sometimes be a challenging undertaking. Ideally, a unit test suite should act as executable documentation and be robust when faced with refactoring of the SUT. Some software lends itself quite naturally towards clear and concise test code, while other SUTs naturally pull you in the direction of obscure and fragile tests. The remedy is SUT API Encapsulation in the form of helper methods and classes that hide any unnecessary complexity of the SUT during each test.

Such a test-specific API can be implemented with extension methods, which can increase readability of the test code, and as such help ensure that the tests can act as executable documentation.

State machines provide very good examples of the need for SUT API Encapsulation, since the legal state transitions may often force you to take a long detour through intermediate states to be able to test a specific state. As an example, consider a state machine called MyStateMachine, that can be in either of the states A, B, C, or D. In this example, the API and legal state transitions are complex; for instance, to go from A to B, you must invoke a complex method:

public void FirstComplexOperation(DateTime anonymousDate, Something s)

The type Something used as a parameter is in itself a complex type, and to make a legal transition from A to B, you need to provide specific values for the members of that class.

Writing a unit test for the transition from A to B is complex, but manageable. However, the legal transition from B to D is another complex operation involving specific values, so now the unit test is already beginning to look obscure:

[TestMethod]
public void WhenInStateBSettingComplexPropertyWillTransitionToStateD()
{
    // Fixture setup
    MyState expectedState = MyState.D;
 
    string anonymousS = "Anonymous string";
    int i = 8;
    decimal anonymousD = 8.34m;
    Something s = new Something(anonymousS, i, anonymousD);
 
    DateTime anonymousDate = new DateTime(2008, 5, 30);
 
    string anonymousOther = "Other anonymous string";
    bool anonymousBool = true;
    Floobudizer<bool> floob = new Floobudizer<bool>(anonymousOther, anonymousBool);
 
    MyStateMachine sut = new MyStateMachine();
 
    // Need to get into state B first
    sut.FirstComplexOperation(anonymousDate, s);
 
    // Exercise system
    sut.ComplexThing = floob;
    MyState result = sut.State;
    // Verify outcome
    Assert.AreEqual<MyState>(expectedState, result, "State");
    // Teardown
}

When you look at this test, it's not very clear which parts of the fixture setup that are relevant to the test, and which parts are there only to get the SUT into the desired state (B). I've even had to resort to an apology to explain that FirstComplexOperation is being invoked to transition to state B. Imagine what a test will be like if you want to test the transition from D to C in this way: Not very readable.

A conventional implementation of SUT API Encapsulation involves moving all the irrelevant fixture setup code to a utility method: In this case a private static method on the test class called MoveToStateB. It takes the SUT as a parameter, as well as the integer called i in the previous test; imagine that this parameter is a critical parameter that I need to vary in different tests. While I'll leave the implementation of MoveToStateB to the interested reader, here's how the refactored test looks:

[TestMethod]
public void WhenInStateBSettingComplexPropertyWillTransitionToStateDRefactored()
{
    // Fixture setup
    MyState expectedState = MyState.D;
 
    string anonymousOther = "Other anonymous string";
    bool anonymousBool = true;
    Floobudizer<bool> floob = new Floobudizer<bool>(anonymousOther, anonymousBool);
 
    MyStateMachine sut = new MyStateMachine();
    MyStateMachineTest.MoveToStateB(sut, 8);
 
    // Exercise system
    sut.ComplexThing = floob;
    MyState result = sut.State;
    // Verify outcome
    Assert.AreEqual<MyState>(expectedState, result, "State");
    // Teardown
}

Better, but still not quite there...

While it's more obvious what's going on (I got rid of the apology, among other improvements), the sut parameter sort of drowns in the call to MoveToStateB, as it just sits there as one among other parameters to the method call. If the number of parameters had been larger it would only have exacerbated this issue.

This is where extension methods come in handy. Instead of implementing MoveToStateB as a private utility method, I can implement it as an extension method (again, I'll leave the details to the interested reader), and the test now looks like this:

[TestMethod]
public void WhenInStateBSettingComplexPropertyWillTransitionToStateDRefactoredWithExtensionMethod()
{
    // Fixture setup
    MyState expectedState = MyState.D;
 
    string anonymousOther = "Other anonymous string";
    bool anonymousBool = true;
    Floobudizer<bool> floob = new Floobudizer<bool>(anonymousOther, anonymousBool);
 
    MyStateMachine sut = new MyStateMachine();
    sut.MoveToStateB(8);
 
    // Exercise system
    sut.ComplexThing = floob;
    MyState result = sut.State;
    // Verify outcome
    Assert.AreEqual<MyState>(expectedState, result, "State");
    // Teardown
}

The number of code lines are the same as before, but this is much more communicative, since it's immediately obvious that the sut variable is the one being modified.

While it's obvious to think that I could have done the same by deriving from MyStateMachine and added MoveToStateB to the derived class, there are subtle differences:

  • You wouldn't be testing the real class, but a derived class that only exists for testability purposes.
  • You can't be sure that someone else doesn't come along at a later date and overrides a virtual method on the SUT. This could change the behavior of the SUT and break the test. Worst of all, it may produce a false negative.
  • This approach doesn't work if the SUT is sealed.

Extension methods comes with the overhead of managing new, test-specific namespaces, since you should always put extension methods in a separate namespace, but used wisely, they are a great addition to your arsenal of unit testing techniques.

Posted by ploeh | 0 Comments
Filed under:

Declarative Use of Custom SecurityTokenParameters

It's not the first time I've stated this, but one of the reasons I love WCF is that it's so wonderfully extensible. You can even implement your own custom security token, as this article explains. The only problem with this article is that it uses imperative code to create a custom Binding, and it doesn't explain how you can implement a custom security token mechanism in a declarative way (i.e. using app.config).

The offending part is the custom SecurityTokenParameters, which you can't specify declaratively, but have to attach to a SecurityBindingElement in some way.

So if you still want to be able to specify the use of your custom security token in app.config, how can you implement that?

A simple solution I've found involves creating a custom BindingElement that contains all the custom security token implementation, including the custom SecurityTokenParameters. This BindingElement additionally acts as a Decorator for whatever SecurityBindingElement you really want to use:

public class CreditCardSecurityBindingElement : BindingElement
{
    private readonly SymmetricSecurityBindingElement innerBindingElement_;
 
    public CreditCardSecurityBindingElement()
    {
        this.innerBindingElement_ = new SymmetricSecurityBindingElement();
        this.innerBindingElement_.EndpointSupportingTokenParameters.SignedEncrypted.Add(new CreditCardTokenParameters());
 
        //..
    }
 
    //..
}

To fully implement your custom BindingElement, remember to override all its virtual methods to delegate the functionality to the inner SecurityBindingElement, like this:

public override T GetProperty<T>(BindingContext context)
{
    return this.innerBindingElement_.GetProperty<T>(context);
}

To be able to use your custom BindingElement (CreditCardSecurityBindingElement) declaratively as part of a custom binding in app.config, you need to implement a BindingElementExtensionElement that creates it. Once you have done that, you should be good to go.

Posted by ploeh | 1 Comments
Filed under:

Unit Testing Anti-Pattern #2: Not Covering Bugs In Your Tests With Tests

Roy just posted a unit testing anti-pattern based on his personal experience. I'd like to follow up with one from my own personal experience:

A few days ago, while working with one of our unit test suites, I discovered that one of my test utility classes had a bug. The bug didn't cause any tests to fail, but it caused a few tests to become false negatives - they didn't fail, but they actually didn't test what they were supposed to test either. Embarrassed, I corrected the test utility class (it was a very simple fix), all unit tests still succeeded, and now the tests that previously didn't test the correct behavior actually do that.

At that point, I happily checked in my changes and went along with my life.

Later, it occurred to me that I couldn't be certain that the fix I had implemented would stay that way. What if someone else comes along and changes the test utility class back to the way it was before? I know from experience that it isn't going to break any tests, although some tests do rely on specific behavior of the test utility class.

When I first wrote the test utility class, I thought it was so simple that it didn't need testing in itself. When I fixed it, it was still simple, but if I could make the mistake once, I could conceivably make it again, so I ought to protect myself against this sort of regression. As Gerard Meszaros writes in xUnit Test Patterns, you may definitely have a need for writing Test Utility Tests, and that was obviously the correct thing to do here, so I wrote a test that verifies that the test utility class behaves as expected, and I can now feel much more confident that my 'real' tests will not produce false negatives.

So, following Roy's pattern language, anti-pattern #2:

Forces: The bug is in your test code, so you just want to fix the test and get on with the 'real' feature you are working on.

Behavior:

You don't think test code needs testing, even when you just discovered a bug there. Instead of writing a test that reproduces (or exposes) the test bug, you just fix the test code and hope it's going to stay fixed forever.

How to avoid: Apply the same mindset to your test code as you do to your production code: If you discover a bug in the test code, write one or more unit tests that reproduces the bug, and then you can fix it. The ensures that no test regressions will occur duing a later test refactoring.

It would be interesting to read about other unit testing anti-patterns, so I'd hereby like to start a little relay race and pass the baton to Ayende, whom I much admire, in the vain hope that he will divert us with unit-testing anti-pattern #3, and subsequently pass on the baton to someone else...

Posted by ploeh | 0 Comments
Filed under:

Vote For Better TDD Support in Visual Studio 2008

Eric Jorgensen has created a Microsoft Connect item asking for better TDD support in Visual Studio 2008. If you also miss the green (sometimes red) progress bar and other UI elements of NUnit and similar tools, and think that Visual Studio should have a UI that supports TDD better than it currently does, please go to the item and give it a favorable vote!
Posted by ploeh | 1 Comments
Filed under:

Automatically Generating LINQ To SQL Models From T-SQL

With LINQ to SQL, a couple of questions quickly arise:

  • Since you can create a data model directly in Visual Studio, where's your authoritative definition of the database?
  • If you generate a data model from your database, how do you maintain the model if you have to tweak it?

The first question is pretty easy to answer: Since T-SQL can express a lot more about your database than can a LINQ data model, T-SQL should be your authoritative definition. For example, you can define clustered and non-clustered indexes in T-SQL, but not in LINQ to SQL (as far as I know).

Since T-SQL should be your authoritative database definition, you can generate a LINQ to SQL data model from your database. Unfortunately, there's no tool that I'm aware of that you can point directly at a set of T-SQL scripts and have it generate a .dbml directly, so you have to have a SQL database your tool can use.

The next question that pops up, then, is how do you ensure that this particular database instance reflects your T-SQL script?

The answer to that question is to create the database just before you run your tool. If you've been following my posts on data access testing, you will know that I'm not in the habit of having specific databases on my machine - I have a database engine (SQL Server) on my machine, but no databases. When I run a test suite, it first creates a completely new test-specific, temporary database, runs all the tests, and finally deletes the database again.

When generating or updating the .dbml file, you can follow the same approach, and you can automate the whole thing in a script. The script should perform the following steps:

  • Create the database based on the authoritative T-SQL scripts.
  • Generate the data model using Sqlmetal.exe.
  • Modify the .dbml file if you need to customize it (such as changing generated property names, etc.)
  • Delete the database.

Using my HoneySqlAccess project as an example, let's have a look at how to accomplish this. I'll use a Visual Studio 2008 PowerShell to script this, but you can also do it in a Command prompt if you can find a command-line XSLT processor.

The first step is to create the database itself, complete with schema. Since I use an Installer for this, I do it with InstallUtil, but you could also execute a set of T-SQL scripts by using SqlCmd:

InstallUtil /ConnectionString="Data Source=localhost;Initial Catalog=HoneyTemp;Integrated Security=True" bin\Debug\Ploeh.Samples.HoneySqlAccess.dll /LogFile=

The main thing to notice here is that I create a new database called HoneyTemp.

The next step is to create the LINQ to SQL data model. For that purpose, we can use a tool called Sqlmetal:

SqlMetal /server:localhost /database:HoneyTemp /dbml:OriginalHoneyDB.dbml /namespace:Ploeh.Samples.HoneySqlAccess /pluralize

This creates the file OriginalHoneyDB.dmbl from the database HoneyTemp that was just created in the step before.

If you are inclined to accept the generated data model as is, you can just include the file in your project and go ahead and use it. When you add a .dbml file to Visual Studio 2008, it will automatically set it up with the MSLinqToSQLGenerator custom tool, which will cause it to autogenerate .NET code for you.

In many cases, however, you will want to tweak the model a bit (I'll get back to that later). Since a .dbml file is just an XML file, the most flexible approach is to apply an XSL transformation. Since this is PowerShell, I can just use .NET objects directly:

$transformation = [System.IO.Path]::Combine((Get-Location).Path, "ModifyDataModel.xslt")
$originalDbml = [System.IO.Path]::Combine((Get-Location).Path, "OriginalHoneyDB.dbml")
$dbml = [System.IO.Path]::Combine((Get-Location).Path, "HoneyDB.dbml")
 
$xslt = New-Object System.Xml.Xsl.XslCompiledTransform
$xslt.Load($transformation)
$xslt.Transform($originalDbml, $dbml)

This part of the script uses the ModifyDataModel.xslt transformation to create the HoneyDB.dbml file that I can then go ahead and include in my Visual Studio project.

Left is only a bit of clean-up:

del $originalDbml
 
InstallUtil /u /ConnectionString="Data Source=localhost;Initial Catalog=HoneyTemp;Integrated Security=True" bin\Debug\Ploeh.Samples.HoneySqlAccess.dll /LogFile=

The OriginalHoneyDB.dbml file is just for transitory use, so can be deleted. The last line is just my (Installer-based) way of removing the database from my system again (notice the /u switch to uninstall).

Most of this script just calls .NET SDK command-line utilities, or, if you want to use SqlCmd for executing T-SQL scripts, tools from the SQL Server Client Tools installation. The only part that isn't going to work in cmd.exe is the XSLT processing part, but if you can find a suitable command-line XSLT processor (there are several free ones available), you can mimic this script in a .bat file as well.

The last thing I want to show you is a couple of snippets from the XSL transformation.

Since the transitory database was called HoneyTemp, Sqlmetal creates a DataContext called HoneyTemp. That is obviously not an acceptable name, so it should be renamed to something better, like HoneyContext:

<xsl:template match="dbml:Database">
  <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:attribute name="Name">HoneyContext</xsl:attribute>
    <xsl:apply-templates select="node()" />
  </xsl:copy>
</xsl:template>

Another modification I wanted to do was to rename Bee.ID to Bee.Id, since Code Analysis would otherwise complain about the naming of the ID property. On the other hand, I didn't want to change the column name in the database from ID to Id, so a mapping is in order. LINQ to SQL supports that scenario by adding a Member attribute to the appropriate Column element, so that's what's going on here:

<xsl:template match="dbml:Database/dbml:Table/dbml:Type[@Name = 'Bee']/dbml:Column[@Name = 'ID']">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()" />
    <xsl:attribute name="Member">Id</xsl:attribute>
  </xsl:copy>