Skip to content

Commit 8c5c9cb

Browse files
committed
Allowed custom step title via attribute on Fluent
1 parent 7cdd716 commit 8c5c9cb

4 files changed

Lines changed: 30 additions & 2 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ public void MethodCallInStepTitle()
1616
var story = this
1717
.Given(_ => GivenWeMutateSomeState())
1818
.When(_ => something.Sub.SomethingHappens())
19+
.And(_ => something.Sub.SomethingWithDifferentTitle())
1920
.Then(_ => ThenTitleHas(AMethodCall()))
2021
.BDDfy();
2122

22-
story.Scenarios.Single().Steps.ElementAt(2).Title.ShouldBe("Then title has Mutated state");
23+
story.Scenarios.Single().Steps.ElementAt(2).Title.ShouldBe("And different title");
24+
story.Scenarios.Single().Steps.ElementAt(3).Title.ShouldBe("Then title has Mutated state");
2325
}
2426

2527
public class FooClass
@@ -38,6 +40,11 @@ public void SomethingHappens()
3840
{
3941

4042
}
43+
44+
[StepTitle("Different title")]
45+
public void SomethingWithDifferentTitle()
46+
{
47+
}
4148
}
4249

4350
private string AMethodCall()

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,12 @@ private StepTitle CreateTitle(string stepTextTemplate, bool includeInputsInStepT
151151
{
152152

153153
var flatInputArray = inputArguments.Select(o => o.Value).FlattenArrays();
154-
var stepTitle = AppendPrefix(Configurator.Scanners.Humanize(methodInfo.Name), stepPrefix);
154+
var name = methodInfo.Name;
155+
var stepTitleAttribute = methodInfo.GetCustomAttributes(typeof(StepTitleAttribute), true).SingleOrDefault();
156+
if (stepTitleAttribute != null)
157+
name = ((StepTitleAttribute)stepTitleAttribute).StepTitle;
158+
159+
var stepTitle = AppendPrefix(Configurator.Scanners.Humanize(name), stepPrefix);
155160

156161
if (!string.IsNullOrEmpty(stepTextTemplate)) stepTitle = string.Format(stepTextTemplate, flatInputArray);
157162
else if (includeInputsInStepTitle)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace TestStack.BDDfy
4+
{
5+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
6+
public class StepTitleAttribute : Attribute
7+
{
8+
public StepTitleAttribute(string stepTitle)
9+
{
10+
this.StepTitle = stepTitle;
11+
}
12+
13+
public string StepTitle { get; private set; }
14+
}
15+
}

TestStack.BDDfy/TestStack.BDDfy.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
<Compile Include="Scenario.cs" />
131131
<Compile Include="Engine.cs" />
132132
<Compile Include="StepTitle.cs" />
133+
<Compile Include="StepTitleAttribute.cs" />
133134
<Compile Include="Story.cs" />
134135
<Compile Include="StoryAttribute.cs" />
135136
<Compile Include="Scanners\StoryMetadata.cs" />

0 commit comments

Comments
 (0)