Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6219867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:51:30+00:00 2026-05-24T07:51:30+00:00

I am still new to mocking and I am having trouble with this code:

  • 0

I am still new to mocking and I am having trouble with this code:

//create the request
SendEmailFromTemplateRequest emailUsingTemplateReq = 
   new SendEmailFromTemplateRequest
   {
       Target = email,
       TemplateId = new Guid("07B94C1D-C85F-492F-B120-F0A743C540E6"),
       RegardingId = toParty[0].PartyId.Id,
       RegardingType = toParty[0].PartyId.LogicalName
   };

//retrieve response
SendEmailFromTemplateResponse emailUsingTemplateResponse =
    (SendEmailFromTemplateResponse)service.Execute(emailUsingTemplateReq);

var emailId = emailUsingTemplateResponse.Id;

I have had no problems up to this point mocking the IOrganizationService, but I am doing something wrong with the execute method. According to the sdk the Execute method returns an OrganizationResponse object that needs to be cast into the correct response class. Here is what I have tried so far:

var idResults = new ParameterCollection();
idResults.Add("Id", Guid.NewGuid());

mockOrganizationService
  .Setup(os => os.Execute(It.IsAny<SendEmailFromTemplateRequest>()))
  .Returns(new OrganizationResponse
  {
      Results = idResults,
      ResponseName = "SendEmailFromTemplate",
  });

When I try to run the test I keep getting an invalid cast exception. I figure I must be setting up the response object wrong. Can someone explain to me the correct way to mock the IOrganizationService.Execute method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:51:31+00:00Added an answer on May 24, 2026 at 7:51 am

    Your approach is correct, but you use the wrong response type. The service returns the results as OrganizationResponse (which is the base class for all responses). You try to cast the base type into a specific type. This doesn’t work.

    You simply have to return an instance of SendEmailFromTemplateResponse to get your code working.

    var orgService = new Mock<IOrganizationService>();
    
    var idResults = new ParameterCollection
    {
       {"Id", Guid.NewGuid()}
    };
    
    orgService.Setup(s => s.Execute(It.IsAny<SendEmailFromTemplateRequest>()))
                           .Returns(new SendEmailFromTemplateResponse
    {
       Results = idResults,
       ResponseName = "SendEmailFromTemplate"
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Still new to Objective C, and I'm having some trouble that I just can't
I'm still new to Objective-C and having some difficulties trying to figure out the
I'm still new top rspec, so please excuse if this is an easy one
I am still new to this Memory Management stuff (Garbage Collector took care of
I am still new to objective-c and am trying to figure out what this
I am still new to Python and have been reviewing the following code not
I'm still new and trying to create a list for use in a function
I am still new to asp.net and I'm having a problem that I just
Still new to .NET, but this looks OK to me. I can't figure out
Im still new to this android stuff but I am trying to write a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.