jordimontana82 / fake-xrm-easy

The testing framework for Dynamics CRM and Dynamics 365 which runs on an In-Memory context and deals with mocks or fakes for you
https://dynamicsvalue.com/get-started/overview?source=git
Other
263 stars 182 forks source link

ServiceClient not working with CrmServiceContext from Early Bound Generated Class #606

Closed lcbotha-braintree closed 9 months ago

lcbotha-braintree commented 9 months ago

Describe the bug I'm trying to use FakeXrmEasy to mock the context and create an IOrganizationService which is then used by the rest of the C# .net core web API functions to query data. The existing calls uses the CrmServiceContext to query the data from dataverse while it seems like if we pass in the service client created by FakeXrmEasy, it doesn't work and we get an error querying the data.

To Reproduce

public class GetPositionSeatsQueryHandlerTest : FakeXrmEasyTestBase
    {
        private readonly bthr_PositionSeat _seat;
        private readonly bthr_Employee _employee;

        public GetPositionSeatsQueryHandlerTest( )
        {
            _employee = new bthr_Employee()
            {
                Id = Guid.NewGuid(),
                bthr_FirstName = "Peter",
                bt_FullName = "Peter Rabbit"
            };

            _seat = new bthr_PositionSeat()
            {
                Id = Guid.NewGuid(),
                bthr_PositionSeatId = Guid.NewGuid(),
                bthr_SeatName = "123",
                bthr_Description = "CEO",
                bt_HasChildren = false,
                bt_SeatStatus = bt_positionseat.Filled,
                bt_Manager = false,
                cr1f2_Department = new EntityReference()
                {
                    Id = Guid.NewGuid(),
                    Name = "Department"
                },
                cr1f2_Division = new EntityReference()
                {
                    Id = Guid.NewGuid(),
                    Name = "Division"
                },
                cr1f2_LegalEntity = new EntityReference()
                {
                    Id = Guid.NewGuid(),
                    Name = "LegalEntity"
                },
                bthr_Employee = _employee.ToEntityReference()
            };
        }

        [Fact]
        public async Task Should_Not_Throw_Exception( )
        {
            _context.Initialize(new List<Entity>() {
                _employee,
                _seat
            });

            var handler = new GetPositionSeatsQueryHandler();
            var @event = new GetPositionSeatsQuery
            {
                TenantServiceClient = _service
            };

            var data1 = _context.CreateQuery<bthr_PositionSeat>().FirstOrDefault();
            // Works. Using the IOrganizationService then for the CrmServiceContext seems to then fail next.

            var crmServiceContext = new CrmServiceContext(_service);
            var data2 = crmServiceContext.CreateQuery<bthr_PositionSeat>().FirstOrDefault();
            // Fails with error: System.ArgumentNullException : Value cannot be null. (Parameter 'key')
        }
    }

Expected behavior The IOrganizationService from FakeXrmEasy does not work when using it within the CrmServiceContext generated by Early Bound Generators.

FakeXrmEasy and Dynamics 365 / CRM version FakeXrmEasy.v9 (3.3.3)

lcbotha-braintree commented 9 months ago

I've managed to resolve it. If anyone else experience the issue its because you need to use the ToEntityRefernce function instead and not manually assign the EntityReference like I did.

jordimontana82 commented 9 months ago

@lcbotha-braintree Glad you got it working. I believe it must be the same issue as https://github.com/DynamicsValue/fake-xrm-easy/issues/107.

Btw this repo is for legacy version v1, the new versions (2.x and 3x) live in several repos under the DynamicsValue organisation: https://github.com/DynamicsValue/fake-xrm-easy