Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

Unit testing issue

(0) ShareShare
ReportReport
Posted on by

Hi folks

I am new to contributing to a Dynamics 365 application and looking to add some unit tests around a method I am working on but have an issue that is baffling me.

I have used a library called FakeXrmEasy.9 to create a fake XrmFakedContext and added a test Email to this as below:

    [TestClass]
    public class AutoSendEmail       
    {
        XrmFakedContext _fakeContext;
        XrmServiceContext _ctx;
        
        private Guid _testScheduledTodayEmail = Guid.NewGuid();
        
        [TestInitialize]
        public void SetUp()
        {
            var dueEmail = new Email
            {
                ActivityId = id,
                Id = id,
                Category = "test category"
                .
                .
                .
            };
                _fakeContext.Initialize(new List()
                { 
                    dueEmail
                });
                _ctx = new XrmServiceContext(_fakeContext.GetOrganizationService());             
        }
        [TestMethod]
        public void Cancel_1st_chaser_email_when_account_payment_method_NOT_invoice()
        {
            internalCodeLibrary.AutoSendMail(_ctx, false);
        }        
    }

So the test simply calls my internal codebase to execute a method and during the course of this method a call is made to ctx.IsAttached for my email which always returns false, however, the existing code then tries to attach my mail and receives exception that "The context is already tracking a different 'email' entity with the same identity"

I don't understand how I can return false for IsAttached then exception that my mail is already being tracked. Any pointers gratefully received.

Cheers

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,095 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans