Skip to content

Commit e7d1ef0

Browse files
author
Jake Ginnivan
committed
Fixing issue with argument extractor not working when drilling into subject
1 parent dfbd050 commit e7d1ef0

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

TestStack.BDDfy.Tests/Scanner/FluentScanner/ExpressionExtensionsTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Linq.Expressions;
44
using NUnit.Framework;
5+
using Shouldly;
56

67
namespace TestStack.BDDfy.Tests.Scanner.FluentScanner
78
{
@@ -64,6 +65,15 @@ public void MethodWithInputs(ContainerType subContainer)
6465
{
6566

6667
}
68+
69+
public Bar Foo { get; set; }
70+
71+
public class Bar
72+
{
73+
public void Baz()
74+
{
75+
}
76+
}
6777
}
6878

6979
object[] GetArguments(Expression<Action<ClassUnderTest>> action, ClassUnderTest instance)
@@ -243,6 +253,13 @@ public void MethodCallValue()
243253
AssertReturnedArguments(arguments, new object[] { 3, "Foo" });
244254
}
245255

256+
[Test]
257+
public void DeepPropertyCall()
258+
{
259+
var arguments = GetArguments(x => x.Foo.Baz(), new ClassUnderTest());
260+
arguments.ShouldBeEmpty();
261+
}
262+
246263
private string GetFooString()
247264
{
248265
return "Foo";

TestStack.BDDfy.Tests/Scanner/FluentScanner/StepTitleTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,32 @@ public class StepTitleTests
1212
[Test]
1313
public void MethodCallInStepTitle()
1414
{
15+
FooClass something = new FooClass();
1516
var story = this
1617
.Given(_ => GivenWeMutateSomeState())
18+
.When(_ => something.Sub.SomethingHappens())
1719
.Then(_ => ThenTitleHas(AMethodCall()))
1820
.BDDfy();
1921

20-
story.Scenarios.Single().Steps.ElementAt(1).Title.ShouldBe("Then title has Mutated state");
22+
story.Scenarios.Single().Steps.ElementAt(2).Title.ShouldBe("Then title has Mutated state");
23+
}
24+
25+
public class FooClass
26+
{
27+
public FooClass()
28+
{
29+
Sub = new BarClass();
30+
}
31+
32+
public BarClass Sub { get; set; }
33+
}
34+
35+
public class BarClass
36+
{
37+
public void SomethingHappens()
38+
{
39+
40+
}
2141
}
2242

2343
private string AMethodCall()

TestStack.BDDfy/Scanners/StepScanners/Fluent/ExpressionExtensions.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static IEnumerable<StepArgument> ExtractArguments<T>(this Expression<Acti
2020
if (methodCallExpression == null)
2121
throw new InvalidOperationException("Please provide a *method call* lambda expression.");
2222

23-
return ExtractArguments(methodCallExpression, value);
23+
return ExtractArguments(methodCallExpression, value, false);
2424
}
2525

2626
public static IEnumerable<StepArgument> ExtractArguments<T>(this Expression<Func<T, Task>> expression, T value)
@@ -33,7 +33,7 @@ public static IEnumerable<StepArgument> ExtractArguments<T>(this Expression<Func
3333
if (methodCallExpression == null)
3434
throw new InvalidOperationException("Please provide a *method call* lambda expression.");
3535

36-
return ExtractArguments(methodCallExpression, value);
36+
return ExtractArguments(methodCallExpression, value, false);
3737
}
3838

3939
private static IEnumerable<StepArgument> ExtractArguments<T>(Expression expression, T value)
@@ -71,21 +71,30 @@ private static IEnumerable<StepArgument> ExtractArguments<T>(Expression expressi
7171
private static IEnumerable<StepArgument> Invoke(MethodCallExpression methodCallExpression, IEnumerable<StepArgument> args)
7272
{
7373
var constantExpression = methodCallExpression.Object as ConstantExpression;
74+
var stepArguments = args.ToArray();
7475
if (constantExpression != null)
75-
return new[] { new StepArgument(() => methodCallExpression.Method.Invoke(constantExpression.Value, args.Select(s => s.Value).ToArray())) };
76+
return new[] { new StepArgument(() => methodCallExpression.Method.Invoke(constantExpression.Value, stepArguments.Select(s => s.Value).ToArray())) };
7677

77-
return new[] { new StepArgument(() => methodCallExpression.Method.Invoke(args.First().Value, args.Skip(1).Select(s => s.Value).ToArray())) };
78+
return new[] { new StepArgument(() =>
79+
{
80+
var value = stepArguments.First().Value;
81+
var parameters = stepArguments.Skip(1).Select(s => s.Value).ToArray();
82+
return methodCallExpression.Method.Invoke(value, parameters);
83+
}) };
7884
}
7985

80-
private static IEnumerable<StepArgument> ExtractArguments<T>(MethodCallExpression methodCallExpression, T value)
86+
private static IEnumerable<StepArgument> ExtractArguments<T>(MethodCallExpression methodCallExpression, T value, bool extractArgsFromExpression = true)
8187
{
8288
var constants = new List<StepArgument>();
8389
foreach (var arg in methodCallExpression.Arguments)
8490
{
8591
constants.AddRange(ExtractArguments(arg, value));
8692
}
8793

88-
constants.AddRange(ExtractArguments(methodCallExpression.Object, value));
94+
if (extractArgsFromExpression)
95+
{
96+
constants.AddRange(ExtractArguments(methodCallExpression.Object, value));
97+
}
8998

9099
return constants;
91100
}

0 commit comments

Comments
 (0)