Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.ArgumentException : Invalid callback. Setup on method with 0 parameter(s) cannot invoke callback with different number of parameters (1) #579

Closed
Saibamen opened this issue Jan 20, 2018 · 10 comments

Comments

@Saibamen
Copy link
Contributor

In my test I get error described in this issue title:

mockedUserRepository.Setup(r => r.GetAll())
                .Returns(new List<DataAccess.Models.User> { user1, user2 }.AsQueryable);

My code for this Setup:

var userList = userRepository.GetAll().Where(x => users.Contains(x.Id)).ToList();

Moq 4.8.1. When I was using 4.7.145 everything was OK.

@stakx
Copy link
Contributor

stakx commented Jan 20, 2018

Possibly a duplicate of #572. Can you please post a minimal but complete test code that reproduces the problem you're describing?

@stakx
Copy link
Contributor

stakx commented Jan 20, 2018

Sorry, but please post minimal but complete (self-contained) repro code here.

@neurohunter
Copy link

neurohunter commented Jun 26, 2018

using System;
using NUnit.Framework;
using Moq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var a = new A();
            Console.WriteLine(a.DoTheThing("thing"));
            Console.ReadKey();
        }
    }

    [TestFixture]
    public class AFixture
    {
        [Test]
        public void DoTheThingTest()
        {
            // this test fails with Invalid callback error
            var a = Mock.Of<IA>();
            var aMock = Mock.Get(a);
            aMock.Setup(i => i.DoTheThing(It.IsAny<string>())).Returns<string>(v => "Not A Thing");
        }

        [Test]
        public void DoAThing()
        {
            // This test passes
            var a = Mock.Of<IA>();
            var aMock = Mock.Get(a);
            aMock.Setup(i => i.DoAThing()).Returns<string>(v => "Not A Thing");
        }
    }

    public interface IA
    {
        string DoAThing();
        string DoTheThing(string a);
    }


    public class A : IA
    {
        public string DoAThing()
        {
            return "A Thing Default";
        }

        public string DoTheThing(string a)
        {
            return "The Thing" + a;
        }
    }
}

@stakx
Copy link
Contributor

stakx commented Jun 26, 2018

@neurohunter - Could you perhaps explain what your post above is about?

@neurohunter
Copy link

@stakx I found this issue with Invalid callback in Moq in my project, found this issue discussion here and noticed that it was closed because no reproduction scenario is provided. So I've basically appended the scenario that I've used to represent the issue.

@stakx
Copy link
Contributor

stakx commented Jun 26, 2018

@neurohunter, OK thanks! I didn't look above your post, I just saw a block of code without any explanation so I was confused enough already to not even look any further. Sorry for that. 😄

@stakx
Copy link
Contributor

stakx commented Jun 26, 2018

@neurohunter - I'm a bit confused however. The test that you've marked as // This test passes fails, and the one you've marked with // this test fails with Invalid callback error passes. Also, I have no clue what the Main method is supposed to do...?

@neurohunter
Copy link

@stakx Yes, you're right, I've added comments later by hand and, apparently, mixed things up.
The correct snippet looks like this:

using System;
using NUnit.Framework;
using Moq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var a = new A();
            Console.WriteLine(a.DoTheThing("thing"));
            Console.ReadKey();
        }
    }

    [TestFixture]
    public class AFixture
    {
        [Test]
        public void DoTheThingTest()
        {
            // This test passes
            var a = Mock.Of<IA>();
            var aMock = Mock.Get(a);
            aMock.Setup(i => i.DoTheThing(It.IsAny<string>())).Returns<string>(v => "Not A Thing");
        }

        [Test]
        public void DoAThing()
        {
            
            // this test fails with Invalid callback error
            var a = Mock.Of<IA>();
            var aMock = Mock.Get(a);
            aMock.Setup(i => i.DoAThing()).Returns<string>(v => "Not A Thing");
        }
    }

    public interface IA
    {
        string DoAThing();
        string DoTheThing(string a);
    }


    public class A : IA
    {
        public string DoAThing()
        {
            return "A Thing Default";
        }

        public string DoTheThing(string a)
        {
            return "The Thing" + a;
        }
    }
}

Main method is not supposed to do anything, it's just there.

@stakx
Copy link
Contributor

stakx commented Jun 26, 2018

OK, in that case this is simply caused by the documented change as mentioned in the changelog:

4.8.0-rc1 (2017-12-08)

Changed

This was requested by #445.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants