NSubstitute icon indicating copy to clipboard operation
NSubstitute copied to clipboard

Dynamic binding with non-dynamic return

Open dtchepak opened this issue 11 years ago • 0 comments

Split from #75.

Example:

        [Test]
        [Pending, Explicit]
        public void AnotherTest()
        {
            // Adapted from: http://stackoverflow.com/questions/13182007/nsubstitute-mock-generic-method/
            var settingsUtil = Substitute.For<ISettingsUtil>();
            // This works:
            //SubstituteExtensions.Returns(settingsUtil.GetConfig<long>("maxImageSize", Arg.Any<dynamic>()), 100L);

            // This fails, with RuntimeBinderException: 'long' does not contain a definition for 'Returns':
            settingsUtil.GetConfig<long>("maxImageSize", Arg.Any<dynamic>()).Returns(100L);

            dynamic settings = new System.Dynamic.ExpandoObject();
            var result = settingsUtil.GetConfig<long>("maxImageSize", settings);
            Assert.That(result, Is.EqualTo(100L));
        }

Discussed here.

Options:

dtchepak avatar Mar 04 '14 03:03 dtchepak