Tanny Xie
Tanny Xie

Reputation: 1

How should I get C# unit test Stub and Shim work with abstract class?

I'm trying to test an abstract class. The goal is to make an abstract method of and an abstract class return some default value. To be more specific, I'm trying to get the GetItemLinqQueryable method of all instances of CosmosDB Container class to return some faked query. GetItemLinqQueryable is the abstract method of Container, which is an abstract class. It's my first time using Shim and Stub. Don't know if it's possible to do that.

I know there are plenty of tutorials out there. I'm looking at the one provided by Microsoft.

This is my abstract class. It is defined in namespace WebApplication1.Controllers

    public abstract class NonStaticClass
    {
        public abstract int NewMethod();

        public virtual int virtualmethod()
        {
            return 0;
        }
    }

In the above doc, it provided this code

var stub = new Fakes.MyClass();
stub.DoAbstractString = (x) => { Assert.IsTrue(x>0); };
stub.DoVirtualInt32 = (n) => 10 ;

I've tried some basic usage of Shim and Stub, and they worked. But for the above example, I cannot find a class with the same name NonStaticClass in my Fakes. If I write the test code like this, it gives error: NonStaticClass doesn't exist in namespace WebApplication1.Controllers.Fakes.

var stub = new WebApplication1.Controllers.Fakes.NonStaticClass();

Could someone tell me how to understand the example code in the tutorial? I'm using MSTest.TestFramework and MSTest.TestAdapter version 3.11. Is this because the version is different?

Upvotes: 0

Views: 63

Answers (0)

Related Questions