Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

How to Mock IorganiationService.Retrieve & RetrieveMultiple methods #68

Closed
krishire opened this issue Jun 2, 2016 · 7 comments
Closed
Labels

Comments

@krishire
Copy link

krishire commented Jun 2, 2016

I have started using FakeXrmEasy but struggling to Mock the retrieve method of the FakedOrganisationService object. Earlier I was using in this fashion base.OrganizationServiceMock.Setup(t =>
t.Retrieve(It.Is(s => s == "systemuser"),
It.IsAny(),
It.IsAny<Microsoft.Xrm.Sdk.Query.ColumnSet>())) // don't care about a specific ColumnSet
.Returns(activeUserNameMock);
Please provide some sample on how to implement the proxy retrieve method.

Basically, In my actual plugin code, I'm retrieving the SystemUser entity for an user in this fashion:
Entity userName = service.Retrieve("systemuser", userId, new Microsoft.Xrm.Sdk.Query.ColumnSet(new[] { "domainname", "fullname" }));

@jordimontana82
Copy link
Owner

jordimontana82 commented Jun 2, 2016

Hi @krishire

You don't need to mock those methods, they are already "mocked", which is the point of the framework.

The only thing you need to do is to get an instance of a fake organization service like this

var context = new XrmFakedContext();
var service = context.GetFakedOrganizationService();

//Then setup your initial state with a single system user, for example
context.Initialize(new List<Entity>() {
    new SystemUser() { Id = Guid.NewGuid(), DomainName = "Domain", Fullname = "Some name" };
});

//After this, any call to service.Retrieve or service.RetrieveMultiple will return that user provided conditions in the query (filters, joins, etc) are satisfied.

More examples here and here

@krishire
Copy link
Author

krishire commented Jun 2, 2016

Many Thanks Jordi, just realised that, thanks for your immediately reply.

@jordimontana82
Copy link
Owner

No worries

@krishire
Copy link
Author

krishire commented Jun 2, 2016

fakedContext.GetDefaultPluginContext().InitiatingUserId and PluginExecutionContext.InitiatingUserId within the plugin code are not matching, any idea.

@jordimontana82
Copy link
Owner

Hi @krishire

Could you please provide a unit test to reproduce this?

From the GetDefaultPluginContext implementation, it should retrieve the one you passed to the context, or, a default one, if it was empty

@krishire
Copy link
Author

krishire commented Jun 3, 2016

Thanks a lot Again Jordi, found the issue in my code, re-initialised the initiatingUserId in the context, after creating the required base record using the default plugin context's intiatingUserId value.

@krishire krishire closed this as completed Jun 3, 2016
@jordimontana82
Copy link
Owner

Grand! You're welcome.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants