Mocking / Faking an Active CRM Object

I use Moq to fake various early related CRM objects so that I can unit test my plugins. I want to fake or mock the Active account, but the problem is that it statecodeis just a read, not a virtual field. For example, when I try to scoff at Accountan early related entity to indicate that it should return, if statecodeavailable, I get:

var accountMock = new Mock<Account>();
accountMock.SetupGet(x => x.statecode).Returns(AccountState.Active);

and NotSupportedExceptionselect: Invalid setup on a non-virtual (overridable in VB) member: x => x.statecode. This is because in the early wrapper class for the account provided by the SDK, the field is statecodenot virtual. Mok cannot redefine it, as I ask him to do! I thought: “Why not make a wrapper for the class Accountthat I have?”.

I could generate the generated code by setting the attribute of statecodeeach object that I want to make fun of virtual, but this will not be saved when / if the wrappers of the object are restored. It also doesn't seem like Moka’s path, but I could be wrong.

My current working reserve is to read a sequential XML account that is already active from a file, but that really breaks the point of ridicule, since basically I have an example data file to read. It works, but it's not a mockery.

TestAccount, , , statecode. , TestAccount, OrganizationService statecode statuscode ( !) , Entity, . , TestAccount . statuscode , statecode , statecode , , statuscode.

!

// Wrapper class for Account so I can mock active and inactive Accounts by changing the statecode and statuscode
public class AccountWrapper : Account
{
    // the member to store our "set statecode" values; only for use in testing and mocking
    private AccountState? _statecode;

    // override and replace the base class statecode
    public new virtual AccountState? statecode
    {
        get { return _statecode; }
        // this is how I intend to get around the read-only of this field in the base class
        // the wrapper pretends to allow the statecode to be set, when it really does not stick on the actual Account entity
        set { _statecode = value; } 
    }
}

mock, Retrieve Account:

var activeAccountMock = new Mock<AccountWrapper>();
activeAccountMock.SetupGet(x => x.statecode).Returns(AccountState.Active);
var serviceMock = new Mock<IOrganizationService>();
serviceMock.Setup(t =>
    t.Retrieve(It.Is<string>(s => s == Account.EntityLogicalName),
               It.IsAny<Guid>(), // don't care about a specific Account
               It.IsAny<ColumnSet>())) // don't care about a specific ColumnSet
     .Returns(activeAccountMock.Object);

, service.Retrieve, , ! Entity statecode , , , Account, statecode . , , , " " null, , , . , , , , , , .

// this guy is type Entity, and its statecode is what I want!
var accountLateBound = service.Retrieve(Account.EntityLogicalName, accountId, new ColumnSet(true));
// accountLateBound["statecode"] is AccountState.Active YAY!
// this clobbers my glorious statecode mock...
Account accountEarlyBound = accountLateBound.ToEntity<Account>();
// accountEarlyBound.statecode is null BOO!

- (.. intellissense, ..). , Moq. !

- Moq? AccountWrapper ? , ? , ... yuck. , , account.statecode = AccountState.[Active|Inactive], SetStateRequest. , , , , , , , - , .

, CRM! , . ,

- ...

tl; dr. CRM , unit test Moq //, ? , ? , ?

+4
1

- . . , , . , CRM, , .

// Wrapper class for Account so I can mock active and inactive Accounts by changing the statecode and statuscode
public class AccountWrapper : Account
{
    // the member to store our "set statecode" values; only for use in testing and mocking
    private AccountState? _statecode;

    // override and replace the base class statecode
    public new virtual AccountState? statecode
    {
        get { return _statecode; }
        // this is to get around the read-only of this field in the base class
        // the wrapper pretends to allow the statecode to be set, when it really does not stick on the actual Account entity
        set
        { 
            _statecode = value;
            if(value == null){
                if(this.Attributes.Contains("statecode")){
                    this.Attributes.Remove("statecode")
                }
            }
            else
            {
                this.SetAttributeValue("statecode", _statecode);
            }
        } 
    }
}

:

CRM , XrmUnitTest, CRM/CDS. , EarlyBoundGenerator XrmToolBox .

+5

Source: https://habr.com/ru/post/1532409/


All Articles