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.
!
public class AccountWrapper : Account
{
private AccountState? _statecode;
public new virtual AccountState? statecode
{
get { return _statecode; }
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>(),
It.IsAny<ColumnSet>()))
.Returns(activeAccountMock.Object);
, service.Retrieve, , ! Entity statecode , , , Account, statecode . , , , " " null, , , . , , , , , , .
var accountLateBound = service.Retrieve(Account.EntityLogicalName, accountId, new ColumnSet(true));
Account accountEarlyBound = accountLateBound.ToEntity<Account>();
- (.. intellissense, ..). , Moq. !
- Moq? AccountWrapper ? , ? , ... yuck. , , account.statecode = AccountState.[Active|Inactive], SetStateRequest. , , , , , , , - , .
, CRM! , . ,
- ...
tl; dr. CRM , unit test Moq //, ? , ? , ?