Ivan Zyranau
Ivan Zyranau

Reputation: 941

How to unit test stateless service in service fabric

I want to create an instance of class A which inherits class StatelessService in my unit test. But I can't. I've tried everything: mocking dependencies, implementing my own contexts and etc.

When I try to create an instance, StatelessService throws NullReferenceException somewhere inside.

Can it be done at all?

class A : StatelessService
{
    public A(StatelessServiceContext context) : base(context /* Here will be thrown NullReferenceException */)
    {
        // It will never even get there.
    }
}

class UnitTest
{
    public void TestMethod()
    {
        var activationContext = MOCK<ICodePackageActivationContext>();
        var context = new StatelessServiceContext(..., activationContext, ...);
        var a = new A(context); // Here will be thrown an exception.
    }
}

Upvotes: 2

Views: 3367

Answers (1)

Esben Bach
Esben Bach

Reputation: 832

It can be done. But instead of re inventing the wheel, have a look at service fabric mocks https://github.com/loekd/ServiceFabric.Mocks

It contains useful helpers for exactly your type of scenario.

Upvotes: 3

Related Questions