1+ using NSubstitute ;
2+ using NSubstitute . ReceivedExtensions ;
3+
4+ namespace LightChain . UnitTests ;
5+
6+ public class ChainTests
7+ {
8+ private List < IProcessor < Input , string > > processors ;
9+ private Chain < IProcessor < Input , string > , Input , string > chain ;
10+
11+ [ SetUp ]
12+ public void Setup ( ) {
13+ processors = new List < IProcessor < Input , string > > {
14+ Substitute . For < IProcessor < Input , string > > ( ) ,
15+ Substitute . For < IProcessor < Input , string > > ( )
16+ } ;
17+ chain = new Chain < IProcessor < Input , string > , Input , string > ( processors ) ;
18+ }
19+
20+ [ Test ]
21+ public void Processor_First_Called_Successfully ( ) {
22+ processors . First ( ) . Condition ( null ) . ReturnsForAnyArgs ( true ) ;
23+ processors . First ( ) . Process ( null ) . ReturnsForAnyArgs ( "some value" ) ;
24+
25+ var input = new Input
26+ {
27+ Value = "some value"
28+ } ;
29+
30+ var result = chain . Run ( input ) ;
31+
32+ Assert . That ( result , Is . EqualTo ( "some value" ) ) ;
33+
34+ processors . First ( ) . Received ( 1 ) . Condition ( input ) ;
35+ processors . First ( ) . Received ( 1 ) . Process ( input ) ;
36+ }
37+
38+ [ Test ]
39+ public void Processor_Second_Called_Successfully ( ) {
40+ processors . First ( ) . Condition ( null ) . ReturnsForAnyArgs ( false ) ;
41+ processors . First ( ) . Process ( null ) . ReturnsForAnyArgs ( "first value" ) ;
42+
43+ processors . ElementAt ( 1 ) . Condition ( null ) . ReturnsForAnyArgs ( true ) ;
44+ processors . ElementAt ( 1 ) . Process ( null ) . ReturnsForAnyArgs ( "second value" ) ;
45+
46+ var input = new Input
47+ {
48+ Value = "some value"
49+ } ;
50+
51+ var result = chain . Run ( input ) ;
52+
53+ Assert . That ( result , Is . EqualTo ( "second value" ) ) ;
54+
55+ processors . First ( ) . Received ( 1 ) . Condition ( input ) ;
56+ processors . First ( ) . DidNotReceive ( ) . Process ( Arg . Any < Input > ( ) ) ;
57+
58+ processors . ElementAt ( 1 ) . Received ( 1 ) . Condition ( input ) ;
59+ processors . ElementAt ( 1 ) . Received ( 1 ) . Process ( input ) ;
60+ }
61+
62+ public class Input
63+ {
64+ public string Value { get ; set ; }
65+ }
66+ }
0 commit comments