English 中文(简体)
Unit Testing in .Net with Endeca Objects
原标题:

Most or all of Endeca s objects have internal constructors. I m working on a good project that lacks great test coverage around the Endeca API, are there any good strategies to unit testing the interactions with Endeca?

So far the best we have is kind of a poor man s adapter pattern:

public class DimValue : IDimValue
{
    public DimValue(Dimension dim, DimVal dimValue)
    {
        Dimension = dim;
        Value = dimValue;
    }

    public virtual bool IsNavigable()
    {
        return Value.IsNavigable();
    }

    public virtual long Id()
    {
        return Value.Id;
    }

    // and so on...
}

We can then mock our own type, DimValue. Is this the best way to keep their API as testable as can be? Or is there another method that is preferred to this?

最佳回答

If you are trying to test your usage of the API, then I would recommend the approach you mentioned.

It is a good design goal to be writing your application built on your own abstractions, not someone else s. The adapter layer gives you a chance to translate the API into a domain language your developers are more comfortable with and gives you the freedom to change technologies later if the product you are using falls short in some way.

Resharper had a great feature for creating wrapper classes. Create a class, add a member that is the type you want to wrap.. then trigger the generate delegates refactoring. Pick all public and there you go.. one wrapped class.

Only expose the subset of their functionality that you are actually using. In your wrapping code, provide interfaces so you can mock.

If you are testing Endeca s API as some form of regression suite so that you can more easily accept new released of their API, then I would just write tests at a more acceptance level; exercising the API they give you. But again, only test the ways you are actually using their API.

I would do the above approach, however...

TypeMock will let you mock classes without interfaces, so that may allow another approach.

Hope this helps.

问题回答

Most Endeca classes are concrete so it is difficult to do unit testing, in our last project we must defined ourselves an abstract layer, exactly it was a facade layer between our own code and Endeca API e.g. IEndecaQuery. With that abstraction layer we could do a test quickly without any real Endeca access, you know opening an Endeca connection will cost you several seconds each time. It was lots of work to implement all necessary Endeca fake objects. Our application was an ecommerce site and we utilized Endeca for all product listing, searching functions.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签