From 672410697a3cfba69d8182571727f6380d28efe5 Mon Sep 17 00:00:00 2001 From: dtchepak Date: Tue, 12 Oct 2010 23:11:54 +1100 Subject: [PATCH] Adding repros for issues #26 and #31 Incorrectly resolving arg matchers with default args (#31) Reentrant setting of return values (#26) Cleaned up code tokens from previous documentation approach. --- .../ParameterMatching.cs | 17 ++++++- .../SimpleSubstituteExamples.cs | 3 -- .../SubstituteTimingAndInteractions.cs | 48 ++++++++++++++----- 3 files changed, 53 insertions(+), 15 deletions(-) diff --git a/Source/NSubstitute.Acceptance.Specs/ParameterMatching.cs b/Source/NSubstitute.Acceptance.Specs/ParameterMatching.cs index 1d0a2a72d..354610d1c 100644 --- a/Source/NSubstitute.Acceptance.Specs/ParameterMatching.cs +++ b/Source/NSubstitute.Acceptance.Specs/ParameterMatching.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using NSubstitute.Exceptions; +using NUnit.Framework; namespace NSubstitute.Acceptance.Specs { @@ -106,6 +107,20 @@ public void Received_should_compare_elements_for_params_arguments() _something.DidNotReceive().WithParams(1, Arg.Is(x => x.Length > 3)); } + [Test] + [Pending] + public void Throw_with_ambiguous_arguments_when_given_an_arg_matcher_and_a_default_arg_value() + { + Assert.Throws(() => + { + _something.Add(0, Arg.Any()).Returns(1); + //Should not make it here, as it can't work out which arg the matcher refers to. + //If it does this will throw an AssertionException rather than AmbiguousArgumentsException. + Assert.That(_something.Add(0, 5), Is.EqualTo(1)); + } + ); + } + [Test] [Pending] public void Resolve_setter_arg_matcher_with_more_specific_type_than_member_signature() diff --git a/Source/NSubstitute.Acceptance.Specs/SimpleSubstituteExamples.cs b/Source/NSubstitute.Acceptance.Specs/SimpleSubstituteExamples.cs index 66456594f..455e3a359 100644 --- a/Source/NSubstitute.Acceptance.Specs/SimpleSubstituteExamples.cs +++ b/Source/NSubstitute.Acceptance.Specs/SimpleSubstituteExamples.cs @@ -6,7 +6,6 @@ namespace NSubstitute.Acceptance.Specs [TestFixture] public class SimpleSubstituteExamples { - /* {CODE:Use_a_shiny_new_substitute} */ [Test] public void Use_a_shiny_new_substitute() { @@ -23,7 +22,6 @@ public void Tell_a_substitute_to_return_a_value() Assert.That(calculator.Add(1, 2), Is.EqualTo(3)); } - /* {CODE:Return_different_values_for_different_arguments} */ [Test] public void Return_different_values_for_different_arguments() { @@ -45,7 +43,6 @@ public void Return_a_value_evaluated_at_runtime() Assert.That(calculator.Add(-1, -2), Is.EqualTo(-3)); } - /* {CODE:Check_a_call_was_received} */ [Test] public void Check_a_call_was_received() { diff --git a/Source/NSubstitute.Acceptance.Specs/SubstituteTimingAndInteractions.cs b/Source/NSubstitute.Acceptance.Specs/SubstituteTimingAndInteractions.cs index aca11bf08..a97983343 100644 --- a/Source/NSubstitute.Acceptance.Specs/SubstituteTimingAndInteractions.cs +++ b/Source/NSubstitute.Acceptance.Specs/SubstituteTimingAndInteractions.cs @@ -3,23 +3,49 @@ namespace NSubstitute.Acceptance.Specs { - [TestFixture] public class SubstituteTimingAndInteractions { - [Test] - public void Update_a_property_on_a_substitute_while_it_is_raising_an_event() + public class When_updating_a_property_on_a_sub_from_an_event_handler { - var firstMate = Substitute.For(); - firstMate.HoistTheMainSail += () => firstMate.IsMainSailHoisted = true; + public interface IICapn + { + event Action HoistTheMainSail; + bool IsMainSailHoisted { get; set; } + } - firstMate.HoistTheMainSail += Raise.Action(); - Assert.That(firstMate.IsMainSailHoisted, Is.True); - } + [Test] + public void Should_update_the_property_when_the_event_is_raised() + { + var firstMate = Substitute.For(); + firstMate.HoistTheMainSail += () => firstMate.IsMainSailHoisted = true; - public interface IICapn + firstMate.HoistTheMainSail += Raise.Action(); + Assert.That(firstMate.IsMainSailHoisted, Is.True); + } + } + + public class When_setting_the_return_value_of_one_sub_within_the_call_to_set_a_return_on_another { - event Action HoistTheMainSail; - bool IsMainSailHoisted { get; set; } + + public interface IWidget { string GetName(); } + public interface IWidgetFactory { IWidget CreateWidget(); } + + [Test][Pending] + public void Should_set_both_calls_to_return_the_specified_values() + { + const string widgetName = "widget x"; + var factory = Substitute.For(); + factory.CreateWidget().Returns(CreateSubstituteForWidget(widgetName)); + + Assert.That(factory.CreateWidget().GetName(), Is.EqualTo(widgetName)); + } + + IWidget CreateSubstituteForWidget(string widgetName) + { + var widget = Substitute.For(); + widget.GetName().Returns(widgetName); + return widget; + } } } } \ No newline at end of file