Unit Test with Proligence Library

Topics: Writing modules
Oct 12, 2015 at 10:45 PM
I created a unit test for an Index action on a controller and it has the following statement
_orchardServices.WorkContext.CurrentUser.As<MyWfgUserPart>().AgentCodeNumber. I need to set the AgentCodeNumber property to a specific value.

I'm using the Proligence Orchard Testing library to assist with unit testing, however, I can't seem to get it to work. I want to be able to something like this:

_orchardServices.WorkContext.CurrentUser.Setup(o => o.As<MyWfgUserPart>().AgentCodeNumber).Returns("ABCDE");

Any help or guidance would be greatly appreciated.
Oct 13, 2015 at 2:39 PM
Hi pairmike,

Users are represented in Orchard as content items, and content items are not that easy to mock. Usually the best way, is to create an in-memory content item, as it will work the same way as a regular content item which is stored in the Orchard database. You can use the ContentFactory helper class from Proligence.Orchard.Testing to easily create content items.

For example, you can achieve your setup using the following code:
// Create a fake content item of the "User" content type (123 is the Id of the content item)
var user = ContentFactory.CreateContentItem<UserPart>(123, "User", new UserPart { Record = new UserPartRecord() }, new MyWfgUserPart(), new InfosetPart());

// Set up the MyWfgUserPart
user.As<MyWfgUserPart>().AgentCodeNumber = "ABCDE";

// Set the fake content item as the current user of the work context
_orchardServices.WorkContext.CurrentUser = user.As<IUser>();
In the future, please post questions to Orchard's GitHub project page (https://github.com/OrchardCMS/Orchard) or Proligence Orchard Testing GitHub project page (https://github.com/Proligence/OrchardTesting). This forum is no longer maintained after Orchard moved to GitHub.
Oct 13, 2015 at 2:59 PM
Thanks, MpDzik. I'll give it a try.