コード例 #1
0
        public void TryCatchWhenTest()
        {
            var expected = new InvalidOperationException("aaa");

            _mock.MethodWithReturn().Throws(expected);
            _mock.Condition.Returns(true);

            var parameter = ExpressionShortcuts.Parameter <IMock>();
            var action    = ExpressionShortcuts.Block()
                            .Parameter(parameter)
                            .Line(parameter.Assign(_mock))
                            .Line(ExpressionShortcuts.Try()
                                  .Body(parameter.Call(o => o.MethodWithReturn()))
                                  .Catch <InvalidOperationException>(
                                      e => ExpressionShortcuts.Null <string>(),
                                      e => e.Call(o => !parameter.Property(x => x.Condition) || o.Message == null)
                                      )
                                  )
                            .Lambda <Func <string> >()
                            .Compile();

            var actual = Assert.Throws <InvalidOperationException>(() => action());

            Assert.Equal(expected, actual);

            _mock.Received(1).MethodWithReturn();
            _ = _mock.Received(1).Condition;
        }
コード例 #2
0
        public void ClosureInCallTest()
        {
            var blockBuilder = ExpressionShortcuts.Block()
                               .Line(ExpressionShortcuts.Call(() => _mock.MethodWithReturn()));

            Expression.Lambda <Func <string> >(blockBuilder).Compile().Invoke();

            _mock.Received(1).MethodWithReturn();
        }