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
OK, so let's say you want to mock SPWeb. Better still you have a method that is going to store properties in the SPWeb PropertyBag (typeof SPPropertyBag) then the method sig to get to update is as follows....

SPWeb web = SPContext.Current.Web;
web.Properties["MyProp"] = "Value";

How would one go about mocking this call? Because I'm having a heck of a time making it work. Here's what I've got so far...

this is what I want to test...

//helper that creates a stub of SPWeb will many properties set. 
Mock<SPWeb> mock = MockSPWebInstance();

Mock<SPPropertyBag> mockBag = MockManager.MockAll<SPPropertyBag>();
mockBag.ExpectCall("Update", 1);

MySettings settings = new MySettings(mock.Object as SPWeb);
settings.Username = "Frank";

This code throws the follow:

System.NullReferenceException: Object reference not set to an instance of an object.
at System.Collections.Specialized.StringDictionary.get_Item(String key)

Any advice, thoughts, wild guesses would be fantastic! Thanks all!
asked by fallenrogue (640 points)

2 Answers

0 votes
OK, I've figured this out but I may have discovered a small bug in TypeMock. Here's my final test:

Mock<SPWeb> mockWeb = MockSPWebInstance();

Mock<SPPropertyBag> mockBag = MockManager.MockObject<SPPropertyBag>(Constructor.StaticNotMocked);

mockBag.ExpectCall("Update", 4);
mockBag.CallBase.ExpectGetAlways("Keys", new[] { "key1", "key2", "key3", "key4" });
mockBag.CallBase.ExpectGetAlways("Values", new[] { "val1", "val2", "val3", "val4"});
mockBag.CallBase.ExpectGetAlways("Count", 4);
mockBag.CallBase.ExpectGetAlways("Item", "True");
mockBag.CallBase.ExpectAndReturn("ContainsKey", true, 4);

mockWeb.ExpectGetAlways("Properties", mockBag.Object);

MySettings settings = new MySettings(mockWeb.Object as SPWeb);
settings.Username = "Frank";

The ISSUE and possible bug was the fact that without this:
mockBag.ExpectCall("Update", 4);

which _was_


I received a NullReferenceException instead of the expected: "Expected 1: Actual 4" on the SPPropertyBag.Update() method.

I hope that help you. If you're getting a nullreferenceexception on a void method signature then you may be calling it too often! :)

I hope that helps someone in the future. Thanks!
answered by fallenrogue (640 points)
0 votes

I couldn't reproduce this here - works as it should.

Let's take it offline. I'll send instructions in a separate email

Note that you can click the following link to learn more on using sharepoint!
answered by gilz (14.5k points)