Search code examples
c#dynamics-crmfakeiteasy

fakeXrmEasy for crm testing initialization issues


I'm trying to follow the basic tutorial for FakeXrmEasy, but I'm not sure why I'm getting errors. I installed everything that needs to be installed to mock Dynamics 365, but I'm still getting errors. I can't figure out what I'm missing, I really want to be able to use this tool.

CS1950 The best overloaded Add method 'List.Add(Entity)' for the collection initializer has some invalid arguments unitTest c:\Users\acapell\documents\visual studio 2015\Projects\unitTest\unitTest\Program.cs 48 Active

CS0246 The type or namespace name 'Account' could not be found (are you missing a using directive or an assembly reference?) unitTest c:\Users\acapell\documents\visual studio 2015\Projects\unitTest\unitTest\Program.cs 45 Active

Didn't know if I was suppose to create an account class, I also tried that but that didn't work either. I got

CS1503 Argument 1: cannot convert from 'unitTest.Account' to 'Microsoft.Xrm.Sdk.Entity' unitTest c:\Users\acapell\documents\visual studio 2015\Projects\unitTest\unitTest\Program.cs 48 Active

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using FakeItEasy;
using FakeXrmEasy;
using Microsoft.Xrm.Sdk;


namespace unitTest
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
     class unitTest
    {
        public object ProxyTypesAssembly { get; private set; }

        public void MyFirstTest()
        {//test method body
            var context = new XrmFakedContext();

            //You can think of a context like an Organisation database which stores entities In Memory.

            //We can also use TypedEntities but we need to tell the context where to look for them, 
            //this could be done, easily, like this:

            context.ProxyTypesAssembly = Assembly.GetAssembly(typeof(Account));

            //We have to define our initial state now, 
            //by calling the Initialize method, which expects a list of entities.



            var account = new Account() { Id = Guid.NewGuid(), Name = "My First Faked Account yeah!" };

            context.Initialize(new List<Entity>() {
               account
            });

        }
    }


}

Solution

  • Do you use early binding in your CRM project and got the references right? If you do not use early binding, you can try late binding, e.g.

    //context.ProxyTypesAssembly = Assembly.GetAssembly(typeof(Account));
    
    var account = new Entity();
    account.LogicalName = "account";
    account.Attributes["name"] = "your account name";