Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't test my plugin because no usersettings found. #358

Closed
TigranBalanyan opened this issue Nov 13, 2018 · 5 comments
Closed

Can't test my plugin because no usersettings found. #358

TigranBalanyan opened this issue Nov 13, 2018 · 5 comments

Comments

@TigranBalanyan
Copy link

Hi, I have a problem here in my plugin, I have a query expression which retrieves "usersettings" entity and has this condition in it

 private int? RetrieveCurrentUsersSettings(IOrganizationService service)
        {
            var currentUserSettings = service.RetrieveMultiple(
                new QueryExpression("usersettings")
                {
                    ColumnSet = new ColumnSet("timezonecode"),
                    Criteria = new FilterExpression
                    {
                        Conditions =
                        {
                            new ConditionExpression("systemuserid", ConditionOperator.EqualUserId)
                        }
                    }
                }).Entities[0].ToEntity<Entity>();

            return (int?)currentUserSettings.Attributes["timezonecode"];
        }

I am getting an error, in my UnitTest, because of this piece of code. Help me to resolve this issue.

Best Regards - Tigran Balanyan

@BetimBeja
Copy link
Contributor

Hello @TigranBalanyan ,
did you initialize the fake context in your test case? You should use Initialize your context before executing you plugin in the UnitTest.
If you can post your UnitTest maybe I can give you feedback on what is wrong.
Best regards,
Betim.

As a side note, personally i think that StackOverflow is more appropriate for your kind of question and here (GitHub) you should make bug reports or feature requests.

@TigranBalanyan
Copy link
Author

Here is my code for the unit tests.

     `   Entity systemUser = new Entity("systemuser");
        systemUser.Id = Guid.NewGuid();
        Entity userSettings = new Entity("usersettings");
        userSettings.Id = Guid.NewGuid();
        userSettings["timezonecode"] = 71;
        userSettings["systemuserid"] = systemUser.ToEntityReference();

        fakedContext.Initialize(new List<Entity>()
        {
            workOrder, owner, invoiceNote, userSettings
        });`

Best Regards - Tigran Balanyan

@BetimBeja
Copy link
Contributor

Hello @TigranBalanyan ,
I am providing you with a functional test case that might give you a hint on what is going wrong with your plugin. My guess is that you are missing fakedContext.CallerId = systemUser.ToEntityReference();

[Fact]
        public void RetrieveUserTimeZoneCode()
        {
            XrmFakedContext fakedContext = new XrmFakedContext();
            Entity systemUser = new Entity("systemuser");
            systemUser.Id = Guid.NewGuid();
            fakedContext.CallerId = systemUser.ToEntityReference();
            IOrganizationService service = fakedContext.GetOrganizationService();
            Entity userSettings = new Entity("usersettings");
            userSettings.Id = Guid.NewGuid();
            userSettings["timezonecode"] = 71;
            userSettings["systemuserid"] = systemUser.ToEntityReference();

            fakedContext.Initialize(userSettings);

            var currentUserSettings = service.RetrieveMultiple(
              new QueryExpression("usersettings")
              {
                  ColumnSet = new ColumnSet("timezonecode"),
                  Criteria = new FilterExpression
                  {
                      Conditions =
                      {
                            new ConditionExpression("systemuserid", ConditionOperator.EqualUserId)
                      }
                  }
              }).Entities[0].ToEntity<Entity>();

            var timezone =  (int?)currentUserSettings.Attributes["timezonecode"];

            Assert.Equal(timezone, 71);
        }

actually i included the piece of code from your plugin directly in the test case but you should test your plugin instead.

Best regards,
Betim.

@TigranBalanyan
Copy link
Author

Thank you, this saved my day. It works.
Happy coding!!!

@jordimontana82
Copy link
Owner

Thanks @BetimBeja for this!

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

No branches or pull requests

3 participants