chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Greetings!
I'm getting an odd "Destination array was not long enough." ArgumentException when I fake a method that has the params keyword. A snippet is worth a thousand words, so observe:
    public class FakeMe
    {
        public string Params(string firstArg, params object[] otherArgs)
        {
            return "I'm real!";
        }
    }

    [TestFixture]
    [Isolated]
    public class Test
    {
        [Test]
        public void Test_Params()
        {
            var fake = Isolate.Fake.Instance<FakeMe>();
            const string iMFake = "I'm fake!";
            Isolate.WhenCalled((string firstArg) => fake.Params(firstArg, "")).AndArgumentsMatch(firstArg => firstArg == "lolwut") .WillReturn(iMFake);

            var result = fake.Params("lolwut");

            Assert.AreEqual(iMFake, result);
        }
    }


Note that the exception occurs on the Isolate.WhenCalled line.

What am I doing wrong?
Thanks!
asked by Guillaume (3.5k points)

2 Answers

0 votes
Hi,

We reproduced this issue and it's a bug.

As a temporary workaround I can suggest using DoInstead:
Isolate.WhenCalled(() => fake.Params(null, null))
    .DoInstead(context => context.Parameters[0] == "lolwut" ? iMFake : string.Empty);

Will this work for you?
answered by Elisha (12k points)
0 votes
Haven't tested it, but that looks very good, I'll be using that. Thanks!
answered by Guillaume (3.5k points)
...