chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi,

I am trying to create a new mock object with configured values.

While setting value on mocked object some property value is not being set.

see below code,

Mock configMock = MockManager.Mock( typeof( MyObj ) );

configMock.ExpectGetAlways( "val1", "1" );
configMock.ExpectGetAlways( "val2", "2" );
configMock.ExpectGetAlways( "val3", "3" );
configMock.ExpectGetAlways( "val4", "4" );

after executing above code ExpectGetAlways function is working properly with val1 and val2 property but it is not working with val3 and val4.

Can anybody help me on this?
asked by ravi.khambhati (800 points)

10 Answers

0 votes
Hi Ravi

The code should work unless val3 and val4 get call on a different instance of MyObj in that case you should create another mock and set expectation on it.

The following test will fail:
Mock configMock = MockManager.Mock(typeof(MyObj));

configMock.ExpectGetAlways("val1", "1");
configMock.ExpectGetAlways("val2", "2");
configMock.ExpectGetAlways("val3", "3");
configMock.ExpectGetAlways("val4", "4");          

MyObj myObj = new MyObj();
Assert.AreEqual("1", myObj.val1);
Assert.AreEqual("2", myObj.val2);

MyObj myObj2 = new MyObj();
Assert.AreEqual("3", myObj2.val3);
Assert.AreEqual("4", myObj2.val4);


Another option is to use Mock.MockAll.
Use MockAll only if you need to set the same behavior on all instances of a class.

Finally I would advise you to use the newer Arrange Act Assert API.
here is the same test using the AAA API:
var fake = Isolate.Fake.Instance<MyObj>();

// use Isolate.Swap.AllInstances<MyObj>();
// incase you want to the same behavior on all instances.
Isolate.Swap.NextInstance<MyObj>().With(fake);

Isolate.WhenCalled(() => fake.val1).WillReturn("1");
Isolate.WhenCalled(() => fake.val2).WillReturn("2");
Isolate.WhenCalled(() => fake.val3).WillReturn("3");
Isolate.WhenCalled(() => fake.val4).WillReturn("4");

MyObj myObj = new MyObj();
Assert.AreEqual("1", myObj.val1);
Assert.AreEqual("2", myObj.val2);
Assert.AreEqual("3", myObj.val3);
Assert.AreEqual("4", myObj.val4);


As you can see the fakes are strongly type and not string base as in the older reflective mocks API.
This can save you from errors that comes from mismatching methods names and of course let you refactor your code easily.
answered by ohad (35.4k points)
0 votes
val1, val2, val3, val4 are static properties of MyObj class. So I am not creating an instance of MyObj class.
answered by ravi.khambhati (800 points)
0 votes
One thing i missed to tell u is that,

val1 and val2 is using SPFarm class and
val3 and val4 is using SPContext.

public static bool val1
        {
            get
            {
                if( SPFarm.Local.FeatureDefinitions[FeatureID].Properties["xyz"] != null )
                {
                    return Convert.ToBoolean( SPFarm.Local.FeatureDefinitions[FeatureID].Properties["xyz"].Value );
                }
                return true;
            }
        }

public static string val3
        {
            get
            {
                return ( string )SPContext.Current.Site.RootWeb.AllProperties["abc"];
             }
        }
answered by ravi.khambhati (800 points)
0 votes
Hi Ravi,

From the code you posted the test should work.
Can you post the full test code plus the definitions of the properties?
answered by ohad (35.4k points)
0 votes
Code that I posted is working on one of my virtual machine and it is not working on my local machine. I think it may be because enviroment is diffrent on my local and vm.

Is this problem is related to GAC?
answered by ravi.khambhati (800 points)
0 votes
Hi Ravi,

It may happen when the machine is 'dirty' i.e previous version was not uninstalled cleanly.

Try to uninstall the current version and than look at the GAC for TypeMock.dll
if its still there delete it from the GAC.
Than install again.
Make sure that the project is referencing the installed Typemock.dll and not some older version.

BTW what version of Isolator are you using?
answered by ohad (35.4k points)
0 votes
I am getting error while running my test case.

System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.SharePoint.Library, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified.

This error I am getting on my Local machine(without moss and wss) but not on virtual machine. I thing problem is with this dll. When I am trying to set value of a static property on class MyObj, it is actually accessing SPContext object and not able to set value of a static property. But when same code runs on my virtual machine(MOSS installed) it works perfectly ok.

Is there any solution to my problem?
answered by ravi.khambhati (800 points)
0 votes
Hi Ravi,
If Microsoft.SharePoint.Library.dll is not installed in the gac try to install it to the gac with the command line utility - gacutil
From the Visual Studio command prompt type:
gacutil -i Microsoft.SharePoint.Library.dll

Having said that, why do you think that the problem is connected to the Isolator?
answered by ohad (35.4k points)
0 votes
There is no dll with name 'Microsoft.SharePoint.Library.dll' in sharepoint.

I have done googled and found,

The sharepoint code will only run on the sharepoint server. It will not help you to copy the dll to your machine - since the DLL doesnt have the framework it needs to run.

So when my test case is executed it dont find SPContext and giving me this error. Can you explain me how can I mock SPContext? I have Typemock Isolator Version 5.0.0.0

Do you have any customer care number so that we can solve this issue on call.
answered by ravi.khambhati (800 points)
0 votes
Hi Ravi,

Lets take it offline.
I'll send you a mail so you can contact us and solve this issue.
answered by ohad (35.4k points)
...