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 7428781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:51:39+00:00 2026-05-29T08:51:39+00:00

I have an interface with a method on it defined like this: IEnumerable<dynamic> ExecuteDynamic(string

  • 0

I have an interface with a method on it defined like this:

IEnumerable<dynamic> ExecuteDynamic(string query, object param = null)

The code I’m trying to testing calls the interface like this:

var result = _repository.ExecuteDynamic("cart_getNewXsession", new
{
    HTTP_USER_AGENT = _context.Request.UserAgent,
    RHID = _context.Request.UserHostAddress,
    scid = GetSourceCode(),
    wsid = _systemSettings.GetWSID(),
    HTTP_REFERER = _context.Request.UrlReferrer == null ? "" : _context.Request.UrlReferrer.ToString(),
    QUERY_STRING = _context.Request.Url.Query
});

I’m trying to mock out the call to this method like this:

var repository = new Mock<>();
var sprocReturnValue = (new[] { (dynamic)new { guid = "123456" } }).AsEnumerable();
repository.Setup(x => x.ExecuteDynamic("cart_getNewXsession", It.IsAny<object>()))
                        .Returns<IEnumerable<dynamic>>(x => sprocReturnValue);

However, when I run this up in a test I get the error:

System.Reflection.TargetParameterCountException : Parameter count mismatch.

    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
    at System.Delegate.DynamicInvokeImpl(Object[] args)
    at System.Delegate.DynamicInvoke(Object[] args)
    at Moq.Extensions.InvokePreserveStack(Delegate del, Object[] args)
    at Moq.MethodCallReturn`2.Execute(ICallContext call)
    at Moq.Interceptor.Intercept(ICallContext invocation)
    at Moq.Proxy.CastleProxyFactory.Interceptor.Intercept(IInvocation invocation)
    at Castle.DynamicProxy.AbstractInvocation.Proceed()
    at Castle.Proxies.IRepositoryProxy.ExecuteDynamic(String query, Object param)
    Services\SessionService.cs(68,0): at Core.Services.SessionService.GetNewXS()
    Services\SessionService.cs(49,0): at Core.Services.SessionService.BuildXS()
    Services\Session\BuildXS.cs(78,0): at Core.Tests.Services.Session.BuildXS.When_XS_In_Cookie_Use_XS_From_Cookie()

In this situation, how should I be setting up my mock object?

  • 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-29T08:51:40+00:00Added an answer on May 29, 2026 at 8:51 am

    The issue is the Returns method.

    The following will work (example in LINQPad):

    void Main()
    {
        var repository = new Mock<IRepository>();
        var sprocReturnValue = (new[] { (dynamic)new { guid = "123456" } }).AsEnumerable();
        repository.Setup(x => x.ExecuteDynamic("cart_getNewXsession", It.IsAny<object>()))
                  .Returns(sprocReturnValue);
    
        var _repository = repository.Object;
        var result = _repository.ExecuteDynamic("cart_getNewXsession", new { Whatever = 0 });
    
        Console.WriteLine(result.First().guid);  // 123456
    }
    
    public interface IRepository
    {
        IEnumerable<dynamic> ExecuteDynamic(string query, object param = null);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object defined like this: Scores.h: @interface Scores : NSObject { NSString
I have a method like this: void m1(string str) and have a class like
I have a method defined in IDL as follows : interface IMyFunc : IDispatch
I have a C# interface with certain method parameters declared as object types. However,
I have an interface called Dictionary which has a method insert() . This interface
The scenario goes like this - I have an assembly say 'MyAssembly'. An interface
I have an interface defined like so: public interface IRipper { IRipperHost Host {
I have two objects defined something like this (simplified for sake of the question):
I have a defined idl file, which looks like this: module Banking { typedef
I have a Sprite object defined as follows: @interface Sprite : NSObject { CGFloat

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.