Amar Gadekar
Amar Gadekar

Reputation: 387

How to get and set value for a object using Shim classes in C# Microsoft fakes unit testing?

Here is my main class which I want to test. It contains one private method.

Public class MyClass
{

    private bool IsWorkDone(MyItem item, string name)
    {
        using (MyThing thingObj = new MyThing(item.ID))
        {
            using (MyWork workObj = thingObj.Open())
            {
                try
                {
                  return false;
                }
            }
        }
        return true;
    }
}

In my test class I have written below method

public void CheckIsWorkDoneTest()
{
    using (ShimsContext.Create())
    {
        MyItem myitem = new ShimMyItem () {
            itemGet = () => new ShimMyItem () {
                IDGet = () => new Guid();                                                                  
            }
        };

        ShimMyClass.AllInstances.isWorkDoneItemString = (MyClass, MyItem, MyName) => "Here I'm stuck. I need help from stackoverflow users"

        PrivateObject objMyClass = new PrivateObject(typeof(MyClass));
        object[] parameters = new object[2] { myItem, workName };
        bool result = Convert.ToBoolean(objMyClass.Invoke("IsWorkDone", parameters));

        Assert.AreEqual(result,true);
    }
}

I want to set the value for oSite object from statement => "using (MyThing thingObj = new MyThing(item.ID)) " from my main MyClass Class. while debugging this line throws Object reference not set to an instance error.

So using ShimMyClass.Allinstance how can I get or set the value for it?

Upvotes: 0

Views: 1299

Answers (1)

doobop
doobop

Reputation: 4520

You have quite a few inconsistencies, so your problem is probably just straitening them out. If you're actual code is more consistent, then update your post. The main things

  • You show private method IsComplete but call isWorkflowCompleted from your Invoke method
  • You probably end up calling your shimmed method that will pass "Here I'm stuck" and try to convert that string to a Boolean.

I fudged your skeleton and got mine to work after adjusting some of the names.

Upvotes: 0

Related Questions