AmbiguousArgumentsException

Namespace: NSubstitute
We found 10 examples in language CSharp for this search. You will see 34 fragments of code.

Other methods

using System;
using System.Runtime.Serialization;

namespace NSubstitute.Exceptions
{
    public class AmbiguousArgumentsException : SubstituteException
    {
        public static string SpecifyAllArguments = "Cannot determine argument specifications to use." + Environment.NewLine +
                                                    "Please use specifications for all arguments of the same type.";
        public AmbiguousArgumentsException() : this(SpecifyAllArguments) { }
        public AmbiguousArgumentsException(string message) : base(message) { }
        protected AmbiguousArgumentsException(SerializationInfo info, StreamingContext context) : base(info, context) { }
    }
}

using System;
using System.Runtime.Serialization;

namespace NSubstitute.Exceptions
{
    public class AmbiguousArgumentsException : SubstituteException
    {
        public static string SpecifyAllArguments = "Cannot determine argument specifications to use." + Environment.NewLine +
                                                    "Please use specifications for all arguments of the same type.";
        public AmbiguousArgumentsException() : this(SpecifyAllArguments) { }
        public AmbiguousArgumentsException(string message) : base(message) { }
        protected AmbiguousArgumentsException(SerializationInfo info, StreamingContext context) : base(info, context) { }
    }
}


        [Test]
        public void Should_add_list_of_all_pending_specifications_to_ambiguous_exception_message()
        {
            var exception = Assert.Throws<AmbiguousArgumentsException>(() =>
            {
                _something.Add(0, Arg.Is(42)).Returns(1);
            });

            Assert.That(exception.Message, Contains.Substring("42"));
        }

        [Test]
        public void Should_fail_with_ambiguous_exception_if_params_boundary_is_crossed_scenario_1()
        {
            var target = Substitute.For<IMethodsWithParamsArgs>();

            Assert.Throws<AmbiguousArgumentsException>(() =>
            {
                target.GetValue(0, Arg.Any<int>()).Returns(42);
            });
        }

        [Test]
        public void Should_fail_with_ambiguous_exception_if_params_boundary_is_crossed_scenario_2()
        {
            var target = Substitute.For<IMethodsWithParamsArgs>();

            Assert.Throws<AmbiguousArgumentsException>(() =>
            {
                target.GetValue(Arg.Any<int>(), 0).Returns(42);
            });
        }

        [Test]
        public void Should_show_already_resolved_matchers_in_ambiguous_exception_v1()
        {
            var foo = Substitute.For<IInterfaceForAmbiguous>();

            var ex = Assert.Throws<AmbiguousArgumentsException>(() =>
            {
                foo.MultipleMixedArgs(Arg.Any<int>(), Arg.Any<double>(), 0, 0d).Returns(42);
            });
            Assert.That(ex.Message, Does.Contain("MultipleMixedArgs(any Int32, any Double, ???, ???"));
        }

        [Test]
        public void Should_show_already_resolved_matchers_in_ambiguous_exception_v2()
        {
            var foo = Substitute.For<IInterfaceForAmbiguous>();

            var ex = Assert.Throws<AmbiguousArgumentsException>(() =>
            {
                foo.MultipleMixedArgs(42, Arg.Any<double>(), 123, 0d).Returns(42);
            });
            Assert.That(ex.Message, Does.Contain("MultipleMixedArgs(42, any Double, 123, ???)"));
        }

        [Test]
        public void Should_show_question_marks_for_non_resolved_matchers_in_ambiguous_exception()
        {
            var foo = Substitute.For<IInterfaceForAmbiguous>();

            var ex = Assert.Throws<AmbiguousArgumentsException>(() =>
            {
                foo.MultipleMixedArgs(Arg.Any<int>(), Arg.Any<double>(), 0, 0).Returns(42);
            });
            Assert.That(ex.Message, Does.Contain("MultipleMixedArgs(any Int32, any Double, ???, ???)"));
        }


        [Test]
        public void Should_add_list_of_all_pending_specifications_to_ambiguous_exception_message()
        {
            var exception = Assert.Throws<AmbiguousArgumentsException>(() =>
            {
                _something.Add(0, Arg.Compat.Is(42)).Returns(1);
            });

            Assert.That(exception.Message, Contains.Substring("42"));
        }

        [Test]
        public void Should_fail_with_ambiguous_exception_if_params_boundary_is_crossed_scenario_1()
        {
            var target = Substitute.For<IMethodsWithParamsArgs>();

            Assert.Throws<AmbiguousArgumentsException>(() =>
            {
                target.GetValue(0, Arg.Compat.Any<int>()).Returns(42);
            });
        }

        [Test]
        public void Should_fail_with_ambiguous_exception_if_params_boundary_is_crossed_scenario_2()
        {
            var target = Substitute.For<IMethodsWithParamsArgs>();

            Assert.Throws<AmbiguousArgumentsException>(() =>
            {
                target.GetValue(Arg.Compat.Any<int>(), 0).Returns(42);
            });
        }


            [Test]
            public void Throw_exception_when_ambiguous_argument_match_by_type()
            {
                Assert.Throws<AmbiguousArgumentsException>(() => sut.Arg<int>());
            }

            [Test]
            public void Throw_when_there_is_no_declared_type_match_but_multiple_compatible_arguments()
            {
                var sut = new CallInfo(new[] { CreateArg<object>("a"), CreateArg<object>("b") });
                Assert.Throws<AmbiguousArgumentsException>(() => sut.Arg<string>());
            }


            [Test]
            public void Throw_exception_when_ambiguous_argument_match_by_type()
            {
                Assert.Throws<AmbiguousArgumentsException>(() => sut.Arg<int>());
            }

            [Test]
            public void Throw_when_there_is_no_declared_type_match_but_multiple_compatible_arguments()
            {
                var sut = new CallInfo(new[] { CreateArg<object>("a"), CreateArg<object>("b") }, null);
                Assert.Throws<AmbiguousArgumentsException>(() => sut.Arg<string>());
            }


        [Test]
        public void Return_result_for_any_argument()
        {
            _something.Echo(Arg.Any<int>()).Returns("anything");

            Assert.That(_something.Echo(1), Is.EqualTo("anything"), "First return");
            Assert.That(_something.Echo(2), Is.EqualTo("anything"), "Second return");
        }

        [Test]
        public void Return_result_for_specific_argument()
        {
            _something.Echo(Arg.Is(3)).Returns("three");
            _something.Echo(4).Returns("four");

            Assert.That(_something.Echo(3), Is.EqualTo("three"), "First return");
            Assert.That(_something.Echo(4), Is.EqualTo("four"), "Second return");
        }

        [Test]
        public void Return_result_for_argument_matching_predicate()
        {
            _something.Echo(Arg.Is<int>(x => x <= 3)).Returns("small");
            _something.Echo(Arg.Is<int>(x => x > 3)).Returns("big");

            Assert.That(_something.Echo(1), Is.EqualTo("small"), "First return");
            Assert.That(_something.Echo(4), Is.EqualTo("big"), "Second return");
        }

        [Test]
        public void Should_not_match_when_arg_matcher_throws()
        {
            _something.Say(Arg.Is<string>(x => x.Length < 2)).Returns("?");

            Assert.That(_something.Say("e"), Is.EqualTo("?"));
            Assert.That(_something.Say("eh"), Is.EqualTo(string.Empty));
            Assert.That(_something.Say(null), Is.EqualTo(string.Empty));
        }

        [Test]
        public void Return_result_with_only_one_matcher_for_that_type()
        {
            _something.Funky(Arg.Any<float>(), 12, "Lots", null).Returns(42);

            Assert.That(_something.Funky(123.456f, 12, "Lots", null), Is.EqualTo(42));
            Assert.That(_something.Funky(0.0f, 12, "Lots", null), Is.EqualTo(42));
            Assert.That(_something.Funky(0.0f, 11, "Lots", null), Is.EqualTo(0));
        }

        [Test]
        public void Received_for_any_argument()
        {
            _something.Echo(7);

            _something.Received().Echo(Arg.Any<int>());
        }

        private static Argument CreateArg<T>(T value) { return new Argument(typeof(T), () => value, x => { }); }

            [Test]
            public void Can_get_arguments()
            {
                Assert.That(sut.Args(), Is.EqualTo(_arguments.Select(x => x.Value).ToArray()));
            }

            [Test]
            public void Can_get_argument_types()
            {
                Assert.That(sut.ArgTypes(), Is.EqualTo(_arguments.Select(x => x.DeclaredType).ToArray()));
            }

            [Test]
            public void Can_get_arguments_by_index()
            {
                for (int i = 0; i < _arguments.Length; i++)
                {
                    Assert.That(sut[i], Is.EqualTo(_arguments[i].Value));
                }
            }

            [Test]
            public void Can_get_argument_by_type()
            {
                var stringArgument = sut.Arg<string>();
                Assert.That(stringArgument, Is.EqualTo("test"));
            }

            [Test]
            public void Throw_exception_when_ambiguous_argument_match_by_type()
            {
                Assert.Throws<AmbiguousArgumentsException>(() => sut.Arg<int>());
            }


        private void EnsureArgIsSettable(Argument argument, int index, object value)
        {
            if (!argument.IsByRef)
            {
                throw new ArgumentIsNotOutOrRefException(index, argument.DeclaredType);
            }

            if (value != null && !argument.CanSetValueWithInstanceOf(value.GetType()))
            {
                throw new ArgumentSetWithIncompatibleValueException(index, argument.DeclaredType, value.GetType());
            }
        }

        public object[] Args()
        {
            return _callArguments.Select(x => x.Value).ToArray();
        }

        public Type[] ArgTypes()
        {
            return _callArguments.Select(x => x.DeclaredType).ToArray();
        }

        public T Arg<T>()
        {
            T arg;
            if (TryGetArg(x => x.IsDeclaredTypeEqualToOrByRefVersionOf(typeof(T)), out arg)) return arg;
            if (TryGetArg(x => x.IsValueAssignableTo(typeof(T)), out arg)) return arg;
            throw new ArgumentNotFoundException("Can not find an argument of type " + typeof(T).FullName + " to this call.");
        }

        private bool TryGetArg<T>(Func<Argument, bool> condition, out T value)
        {
            value = default(T);

            var matchingArgs = _callArguments.Where(condition);
            if (!matchingArgs.Any()) return false;
            ThrowIfMoreThanOne<T>(matchingArgs);

            value = (T)matchingArgs.First().Value;
            return true;
        }

        private void ThrowIfMoreThanOne<T>(IEnumerable<Argument> arguments)
        {
            if (arguments.Skip(1).Any())
            {
                throw new AmbiguousArgumentsException(
                    "There is more than one argument of type " + typeof(T).FullName + " to this call.\n" +
                    "The call signature is (" + DisplayTypes(ArgTypes()) + ")\n" +
                    "  and was called with (" + DisplayTypes(_callArguments.Select(x => x.ActualType)) + ")"
                    );
            }
        }


        public IArgumentSpecification Create(object argument, IParameterInfo parameterInfo, ISuppliedArgumentSpecifications suppliedArgumentSpecifications)
        {
            if (_defaultChecker.IsDefault(argument, parameterInfo.ParameterType))
            {
                if (suppliedArgumentSpecifications.IsNextFor(argument, parameterInfo.ParameterType))
                {
                    var argumentSpecification = suppliedArgumentSpecifications.Dequeue();
                    if (suppliedArgumentSpecifications.DequeueRemaining().Count() == 0)
                    {
                        return argumentSpecification;
                    }
                }
                else if (!suppliedArgumentSpecifications.AnyFor(argument, parameterInfo.ParameterType))
                {
                    if (suppliedArgumentSpecifications.DequeueRemaining().Count() == 0)
                    {
                        return _argumentEqualsSpecificationFactory.Create(argument, parameterInfo.ParameterType);
                    }
                }
            }
            else
            {
                var paramterInfosFromParamsArray = _parameterInfosFromParamsArrayFactory.Create(argument, parameterInfo.ParameterType);
                var suppliedArgumentSpecificationsFromParamsArray = _suppliedArgumentSpecificationsFactory.Create(suppliedArgumentSpecifications.DequeueRemaining());
                var arrayArgumentSpecifications = _arrayArgumentSpecificationsFactory.Create(argument, paramterInfosFromParamsArray, suppliedArgumentSpecificationsFromParamsArray);
                return _arrayContentsArgumentSpecificationFactory.Create(arrayArgumentSpecifications, parameterInfo.ParameterType);
            }
            throw new AmbiguousArgumentsException();
        }

NSubstitute.Exceptions.AmbiguousArgumentsException : ISerializable

Constructors :

public AmbiguousArgumentsException()
public AmbiguousArgumentsException(String message = )
public AmbiguousArgumentsException(MethodInfo method = , IEnumerable<Object> invocationArguments = , IEnumerable<IArgumentSpecification> matchedSpecifications = , IEnumerable<IArgumentSpecification> allSpecifications = )

Methods :

public MethodBase get_TargetSite()
public String get_Message()
public IDictionary get_Data()
public Exception GetBaseException()
public Exception get_InnerException()
public String get_HelpLink()
public Void set_HelpLink(String value = )
public String get_Source()
public Void set_Source(String value = )
public Void GetObjectData(SerializationInfo info = , StreamingContext context = )
public String ToString()
public Int32 get_HResult()
public Void set_HResult(Int32 value = )
public Type GetType()
public String get_StackTrace()
public Type GetType()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()