0

I want to test my service, and i still have this error after hours of trying to find how should i make this right

 public async Task DeleteAsync(string entityId, IEntityRef child)
    {
        Requires.NotNull(entityId, nameof(entityId));
        Requires.NotNull(child, nameof(child));

        await using IAtomicScope atomicScope = _atomicScopeFactory.Create();
        PartnerBase parentPartner = await _partnerStore.GetAsync(entityId, atomicScope);
        IEntityRef parent = new EntityRef
        {
            Id = entityId,
            EntityType = parentPartner.EntityType
        };

        await _validator.ValidateOnDeleteLinkAsync(parent, child);
 
        IContextDetail currentContext = await _contextService.GetCurrentContextAsync();
        PersonModel person = await _verifier.GetAccessibleAsync(child, atomicScope);
        await _verifier.VerifyManagerialPermissionsAsync(currentContext, parent, atomicScope);

        IEntityMembership membership = new EntityMembership
        {
            Parent = parent.ToMembershipEntityRef(),
            Entity = child.ToMembershipEntityRef()
        };

        await _membershipService.DeleteAsync(membership, atomicScope);

        await NotifyMembershipChanged(parent, null, person.UserOpenId, atomicScope);
        await atomicScope.CommitAsync();
    }

and my test

public async void DeleteAsync_ParentNotPersonCorrectCall_MembershipDeleteCalled(EntityType entityType)
    {
        IEntityRef parent = _fixture.Build<EntityRef>()
                                    .With(q => q.EntityType, entityType)
                                    .Create();
        IEntityRef child = _fixture.Create<EntityRef>();
        PersonModel person = _fixture.Create<PersonModel>();

        IEntity entity = _fixture.Create<Department>();

        _verifier.Setup(v => v.GetAccessibleAsync(child, _atomicScope.Object))
                 .ReturnsAsync(person);

        _entityReaderService.SetupGetFirstByReferenceIdAsync(parent.GetReferenceId(), entity, _atomicScope.Object);

        await _service.DeleteAsync(parent.Id, child);

        IEntityMembership membership = new EntityMembership
        {
            Parent = parent.ToMembershipEntityRef(),
            Entity = child.ToMembershipEntityRef()
        };
        _membershipService.Verify(m => m.DeleteAsync(It.Is<IEntityMembership>(q => q.IsEquivalentTo(membership)), _atomicScope.Object), Times.Once);
    }

It throws an error saying

Object reference not set to an instance of an object.

PS: It's my first question, please be kind and don't let it be the last one.. Thanks!

Dilid2
  • 9
  • 2
  • have you used the debugger? can you see on which line you get the exception? If you search that specific error you will see that means that some variable is null. – frankM_DN Oct 17 '22 at 15:47
  • 3
    The remedy for this error is always the same... Find the object variable being dereferenced that is null, and either make it not null or cater to the null value. See https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Robert Harvey Oct 17 '22 at 15:47
  • The problem is at IEntityRef parent = new EntityRef { Id = entityId, EntityType = parentPartner.EntityType }; – Dilid2 Oct 17 '22 at 15:51
  • So `_partnerStore.GetAsync(entityId, atomicScope)` method returns null. You need to check that you arrange everything correctly. – asku Oct 17 '22 at 18:49

0 Answers0