Skip to content

Commit 5437790

Browse files
authored
Merge pull request #344 from robertcoltheart/feature/add-behavior-spec-source
[Feature] Track behavior field from context in behavior spec
2 parents df5ccca + af05faa commit 5437790

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

Source/Machine.Specifications.Specs/Factories/ContextFactorySpecs.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,23 @@ public class and_the_nested_context_is_generic
236236
}
237237
}
238238
}
239+
240+
[Subject(typeof(ContextFactory))]
241+
public class when_creating_behavior_specifications_and_tracking_original_behavior_field
242+
{
243+
static Context newContext;
244+
245+
Establish context = () =>
246+
{
247+
var factory = new ContextFactory();
248+
newContext = factory.CreateContextFrom(new context_with_behaviors());
249+
};
250+
251+
It should_create_behavior_specs_with_original_behavior_field =
252+
() => newContext.Specifications
253+
.OfType<BehaviorSpecification>()
254+
.First()
255+
.BehaviorFieldInfo.Name.Should().BeEquivalentTo("behavior");
256+
257+
}
239258
}

Source/Machine.Specifications/Factories/BehaviorFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public Behavior CreateBehaviorFrom(FieldInfo behaviorField, Context context)
3434
var behavior = new Behavior(behaviorField.FieldType, behaviorInstance, context, isIgnored);
3535

3636
var itFieldInfos = behaviorType.GetInstanceFieldsOfUsage(new AssertDelegateAttributeFullName());
37-
CreateBehaviorSpecifications(itFieldInfos, behavior);
37+
CreateBehaviorSpecifications(itFieldInfos, behaviorField, behavior);
3838

3939
return behavior;
4040
}
@@ -126,11 +126,13 @@ static void EnsureContextFieldIsCompatibleType(Context context, FieldInfo contex
126126
}
127127

128128
void CreateBehaviorSpecifications(IEnumerable<FieldInfo> itFieldInfos,
129+
FieldInfo behaviorField,
129130
Behavior behavior)
130131
{
131132
foreach (var itFieldInfo in itFieldInfos)
132133
{
133134
var specification = _specificationFactory.CreateSpecificationFromBehavior(behavior,
135+
behaviorField,
134136
itFieldInfo);
135137
behavior.AddSpecification(specification);
136138
}

Source/Machine.Specifications/Factories/SpecificationFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public Specification CreateSpecification(Context context, FieldInfo specificatio
1919
return new Specification(name, specificationField.FieldType, it, isIgnored, specificationField);
2020
}
2121

22-
public Specification CreateSpecificationFromBehavior(Behavior behavior, FieldInfo specificationField)
22+
public Specification CreateSpecificationFromBehavior(Behavior behavior, FieldInfo behaviorField, FieldInfo specificationField)
2323
{
2424
bool isIgnored = behavior.IsIgnored || specificationField.HasAttribute(new IgnoreAttributeFullName());
2525
var it = (Delegate)specificationField.GetValue(behavior.Instance);
2626
string name = specificationField.Name.ToFormat();
2727

28-
return new BehaviorSpecification(name, specificationField.FieldType, it, isIgnored, specificationField, behavior.Context, behavior);
28+
return new BehaviorSpecification(name, specificationField.FieldType, behaviorField, it, isIgnored, specificationField, behavior.Context, behavior);
2929
}
3030
}
3131
}

Source/Machine.Specifications/Model/BehaviorSpecification.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,33 @@ namespace Machine.Specifications.Model
88
{
99
public class BehaviorSpecification : Specification
1010
{
11+
readonly FieldInfo _behaviorfield;
1112
readonly object _behaviorInstance;
1213
readonly object _contextInstance;
1314
readonly ConventionMapper _mapper;
1415

1516
public BehaviorSpecification(string name,
1617
Type fieldType,
18+
FieldInfo behaviorfield,
1719
Delegate it,
1820
bool isIgnored,
1921
FieldInfo fieldInfo,
2022
Context context,
2123
Behavior behavior)
2224
: base(name, fieldType, it, isIgnored, fieldInfo)
2325
{
26+
_behaviorfield = behaviorfield;
2427
_contextInstance = context.Instance;
2528
_behaviorInstance = behavior.Instance;
2629

2730
_mapper = new ConventionMapper();
2831
}
2932

33+
public FieldInfo BehaviorFieldInfo
34+
{
35+
get { return _behaviorfield; }
36+
}
37+
3038
protected override void InvokeSpecificationField()
3139
{
3240
_mapper.MapPropertiesOf(_contextInstance).To(_behaviorInstance);

appveyor.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
environment:
2-
nuget_version: '0.11.1'
2+
nuget_version: '0.12.0'
33
nuget_prerelease: false
4-
assembly_version: '0.11.0.0'
4+
assembly_version: '0.12.0.0'
55

66
image: Visual Studio 2017
77

88
deploy:
99
- provider: GitHub
1010
description: |
11-
* Fix ArgumentOutOfRangeException thrown when tests are concurrently writing diagnostic output. (#224 - thanks to dannyvincent)
11+
* Track behavior field from context in behavior specification
1212
1313
on:
1414
appveyor_repo_tag: true

0 commit comments

Comments
 (0)