Commits

Johannes Rudolph committed af12525

Replaced Moq in tests for context setup/teardown failures.

Comments (0)

Files changed (6)

test/SubSpec.Tests/Acceptance Tests/SpecificationDeclarations.cs

                 }
             ";
 
-        XmlNode assemblyNode = ExecuteSpecification(code);
+        XmlNode assemblyNode = ExecuteSpecification( code );
 
         SubSpecResultUtility.VerifyPassed( assemblyNode.AssertionResults().First() );
     }
                 }
             ";
 
-        XmlNode assemblyNode = ExecuteSpecification(code);
+        XmlNode assemblyNode = ExecuteSpecification( code );
 
         SubSpecResultUtility.VerifyPassed( assemblyNode.AssertionResults().First() );
     }
 
         XmlNode assemblyNode = ExecuteSpecification( code );
 
-        string expected = "System.InvalidOperationException : An exception was thrown while building tests from Specification MockTestClass.Spec:\r\nSystem.InvalidOperationException: Specification class does not have a default constructor\r\n";
+        string expected = "System.InvalidOperationException : An exception was thrown while building tests from Specification MockTestClass.Spec:\r\nSystem.InvalidOperationException: Specification class does not have a default constructor";
 
-        XmlNode testNode = assemblyNode.AssertionResults().First();
-
-        SubSpecResultUtility.VerifyFailedWith( testNode, expected );
+        SubSpecResultUtility.VerifyFailedWith( assemblyNode.AssertionResults().First(), expected );
     }
 }

test/SubSpec.Tests/ContextSetupTeardownBehavior.cs

 using System;
 using SubSpec;
 using Xunit;
-using System.Collections.Generic;
-using Moq;
 using Xunit.Sdk;
+using TestUtility;
+using System.Collections;
 
 public class ContextSetupTeardownBehavior
 {
-    public class ContextFixtureSpy
+    private class ContextFixtureSpy
     {
         private bool called = false;
         public void FailWhenCallingTwice()
                 Assert.Throws<InvalidOperationException>( () => sut.FailWhenCallingTwice() ) );
 
         "we expect our first assertion to pass"
-            .Assert( () => 
+            .Assert( () =>
                 Assert.True( true ) );
 
         "we expect the action not to be repeated for the second assertion"
-            .Assert( () =>  
+            .Assert( () =>
                 Assert.True( true ) );
     }
 
         var sut = new ContextFixtureSpy();
 
         "Given a context that may not be established twice".Context( () => { sut.FailWhenCallingTwice(); } );
-        "".Do( () => { } );
 
         "we expect our first assertion to pass".Observation( () => { Assert.True( true ); } );
         "we expect the context instantiation not to be repeated for the second assertion".Observation( () => { Assert.True( true ); } );
     }
 
+    public static void SpecificationThatShouldDisposeItsAssertionFixture()
+    {
+        "Given a disposable Fixture".ContextFixture( () => new DisposeSpy() );
+        "when we encounter an exception during test execution ".Do( () => { throw new Exception( "" ); } );
+        "with Assertions, we expect the context is nonetheless disposed".Assert( () => { } );
+    }
+
     [Fact]
     public void ErrorInDoForAssertionDisposesContext()
     {
-        var m = new Mock<IDisposable>();
+        DisposeSpy.Reset();
 
-        SubSpec.Core.SpecificationContext.Context( "", () => { return m.Object; } );
-        SubSpec.Core.SpecificationContext.Do( "", () => { throw new Exception( "" ); } );
-        SubSpec.Core.SpecificationContext.Assert( "", () => { } );
+        IMethodInfo method = Reflector.Wrap( StaticReflection.MethodOf( () => SpecificationThatShouldDisposeItsAssertionFixture() ) );
 
-        Mock<IMethodInfo> testMethod = new Mock<IMethodInfo>();
-        testMethod.SetupGet( x => x.IsStatic ).Returns( true );
+        ExecuteSpecification( method );
 
-        SpecificationAttribute.FtoEnumerateTestCommands( testMethod.Object ).Consume();
+        Assert.True( DisposeSpy.WasDisposed );
+    }
 
-        m.Verify( x => x.Dispose() );
+    public static void SpecificationThatShouldDisposeItsObservationFixture()
+    {
+        "Given a disposable Fixture".ContextFixture( () => new DisposeSpy() );
+        "when we encounter an exception during test execution ".Do( () => { throw new Exception( "" ); } );
+        "with Observation, we expect the context is nonetheless disposed".Observation( () => { } );
     }
 
     [Fact]
     public void ErrorInDoForObservationDisposesContext()
     {
-        var m = new Mock<IDisposable>();
+        DisposeSpy.Reset();
 
-        SubSpec.Core.SpecificationContext.Context( "", () => { return m.Object; } );
-        SubSpec.Core.SpecificationContext.Do( "", () => { throw new Exception( "" ); } );
-        SubSpec.Core.SpecificationContext.Observation( "", () => { } );
+        IMethodInfo method = Reflector.Wrap( StaticReflection.MethodOf( () => SpecificationThatShouldDisposeItsObservationFixture() ) );
+        ExecuteSpecification( method );
 
-        Mock<IMethodInfo> testMethod = new Mock<IMethodInfo>();
+        Assert.True( DisposeSpy.WasDisposed );
+    }
 
-        testMethod.SetupGet( x => x.IsStatic ).Returns( true ); SpecificationAttribute.FtoEnumerateTestCommands( testMethod.Object ).Consume();
+    private static void ExecuteSpecification( IMethodInfo spec )
+    {
+        foreach (var item in SpecificationAttribute.FtoEnumerateTestCommands( spec ))
+            item.Execute( null );
+    }
 
-        m.Verify( x => x.Dispose() );
-    }
-}
+    private class DisposeSpy : IDisposable
+    {
+        private static bool Disposed = false;
 
-internal static partial class Mixin
-{
-    public static void Consume( this IEnumerable<ITestCommand> seq )
-    {
-        foreach (var item in seq)
-        {   try
-            {
-                item.Execute(null);
-            }
-            catch (Exception ex)
-            {
-                Console.WriteLine(ex);
-            }
+        public static bool WasDisposed
+        {
+            get { return Disposed; }
+        }
+
+        public static void Reset()
+        {
+            Disposed = false;
+        }
+
+        public void Dispose()
+        {
+            Disposed = true;
         }
     }
 }

test/SubSpec.Tests/SubSpec.Tests.csproj

   </PropertyGroup>
   <ItemGroup>
     <Reference Include="Moq, Version=4.0.10827.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
-      <SpecificVersion>False</SpecificVersion>
       <HintPath>..\..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
     </Reference>
     <Reference Include="System" />
     <Compile Include="Acceptance Tests\ContextSetupTeardownFailure.cs" />
     <Compile Include="Acceptance Tests\OutputCapturing.cs" />
     <Compile Include="Acceptance Tests\SpecificationDeclarations.cs" />
-    <Compile Include="SubSpecResultUtility.cs" />
+    <Compile Include="test.utility\SubSpecResultUtility.cs" />
     <Compile Include="Acceptance Tests\TimeoutSpecifications.cs" />
     <Compile Include="CompositeFixtureFacts.cs" />
     <Compile Include="test.utility\MockAssembly.cs" />
     <Compile Include="test.utility\ResultXmlUtility.cs" />
+    <Compile Include="test.utility\StaticReflection.cs" />
     <Compile Include="test.utility\StubExecutorWrapper.cs" />
     <Compile Include="test.utility\StubTestRunner.cs" />
     <Compile Include="test.utility\StubTransformer.cs" />

test/SubSpec.Tests/SubSpecResultUtility.cs

-using System;
-using System.Xml;
-using System.Linq;
-using System.Collections.Generic;
-using Xunit;
-
-namespace TestUtility
-{
-    public static class SubSpecResultUtility
-    {
-        public static XmlNode GetClassResult( XmlNode assemblyNode, int classIndex )
-        {
-            XmlNodeList classNodes = assemblyNode.SelectNodes( "class" );
-            if (classNodes.Count <= classIndex)
-                throw new ArgumentException( "Could not find class item with index " + classIndex + " in XML:\r\n" + assemblyNode.OuterXml );
-
-            return classNodes[classIndex];
-        }
-
-        public static XmlNode GetObservationResult( XmlNode assemblyNode, int observationIndex )
-        {
-            return ObservationResults( assemblyNode ).ElementAt( observationIndex + 1 );
-        }
-
-        public static XmlNode ObservationTeardownResult( this XmlNode assemblyNode )
-        {
-            return GetClassResult( assemblyNode, 0 ).ChildNodes.Cast<XmlNode>().Single( x => x.Attributes.GetNamedItem( "name" ).Value.StartsWith( "}" ) );
-        }
-
-        public static XmlNode ObservationSetupResult( this XmlNode assemblyNode )
-        {
-            return GetClassResult( assemblyNode, 0 ).ChildNodes.Cast<XmlNode>().Single( x => x.Attributes.GetNamedItem( "name" ).Value.StartsWith( "{" ) );
-        }
-
-        public static IEnumerable<XmlNode> ObservationResults( this XmlNode assemblyNode )
-        {
-            return GetClassResult( assemblyNode, 0 ).ChildNodes.Cast<XmlNode>()
-                .SkipWhile( x => !IsObservationSetupNode( x ) ).Skip( 1 )
-                .TakeWhile( x => !IsObservationTeardownNode( x ) )
-                .Select( x => x );
-        }
-
-        private static bool IsObservationSetupNode( XmlNode x )
-        {
-            return x.Attributes.GetNamedItem( "name" ).Value.StartsWith( "{" );
-        }
-        private static bool IsObservationTeardownNode( XmlNode x )
-        {
-            return x.Attributes.GetNamedItem( "name" ).Value.StartsWith( "}" );
-        }
-        public static IEnumerable<XmlNode> AssertionResults( this XmlNode assemblyNode )
-        {
-            return GetClassResult( assemblyNode, 0 ).ChildNodes.Cast<XmlNode>().Except( assemblyNode.ObservationResults() );
-        }
-
-        public static XmlNode Second( this IEnumerable<XmlNode> source )
-        {
-            return source.ElementAt( 1 );
-        }
-        public static XmlNode First( this IEnumerable<XmlNode> source )
-        {
-            return source.First<XmlNode>();
-        }
-
-        public static void VerifyOutput( XmlNode testNode, string expectedOutput )
-        {
-            XmlNode outputNode = testNode.SelectSingleNode( "output" );
-
-            Assert.Equal( expectedOutput, outputNode.InnerXml.Trim() );
-        }
-
-        public static void VerifyPassed( XmlNode testNode )
-        {
-            ResultXmlUtility.AssertAttribute( testNode, "result", "Pass" );
-        }
-
-        public static void VerifyFailed( XmlNode testNode )
-        {
-            ResultXmlUtility.AssertAttribute( testNode, "result", "Fail" );
-        }
-
-        public static void VerifyFailedWith( XmlNode testNode, string expectedFailure )
-        {
-            VerifyFailed( testNode );
-
-            XmlNode firstFailureNode = testNode.SelectSingleNode( "failure" );
-
-            string innerxml = firstFailureNode.SelectSingleNode( "message" ).InnerXml;
-            Assert.True( innerxml.StartsWith( expectedFailure ), String.Format( "\nExpected: {0}\nActual: {1}", expectedFailure, innerxml ) );
-        }
-    }
-}

test/SubSpec.Tests/test.utility/StaticReflection.cs

+using System;
+using System.Reflection;
+using System.Linq.Expressions;
+
+namespace TestUtility
+{
+    internal static class StaticReflection
+    {
+        public static FieldInfo FieldOf<T>( Expression<System.Func<T>> expression )
+        {
+            MemberExpression body = (MemberExpression)expression.Body;
+            return (FieldInfo)body.Member;
+        }
+
+        public static MethodInfo MethodOf<T>( Expression<System.Func<T>> expression )
+        {
+            MethodCallExpression body = (MethodCallExpression)expression.Body;
+            return body.Method;
+        }
+
+        public static MethodInfo MethodOf( Expression<System.Action> expression )
+        {
+            MethodCallExpression body = (MethodCallExpression)expression.Body;
+            return body.Method;
+        }
+    }
+}

test/SubSpec.Tests/test.utility/SubSpecResultUtility.cs

+using System;
+using System.Xml;
+using System.Linq;
+using System.Collections.Generic;
+using Xunit;
+
+namespace TestUtility
+{
+    public static class SubSpecResultUtility
+    {
+        public static XmlNode GetClassResult( XmlNode assemblyNode, int classIndex )
+        {
+            XmlNodeList classNodes = assemblyNode.SelectNodes( "class" );
+            if (classNodes.Count <= classIndex)
+                throw new ArgumentException( "Could not find class item with index " + classIndex + " in XML:\r\n" + assemblyNode.OuterXml );
+
+            return classNodes[classIndex];
+        }
+
+        public static XmlNode GetObservationResult( XmlNode assemblyNode, int observationIndex )
+        {
+            return ObservationResults( assemblyNode ).ElementAt( observationIndex + 1 );
+        }
+
+        public static XmlNode ObservationTeardownResult( this XmlNode assemblyNode )
+        {
+            return GetClassResult( assemblyNode, 0 ).ChildNodes.Cast<XmlNode>().Single( x => x.Attributes.GetNamedItem( "name" ).Value.StartsWith( "}" ) );
+        }
+
+        public static XmlNode ObservationSetupResult( this XmlNode assemblyNode )
+        {
+            return GetClassResult( assemblyNode, 0 ).ChildNodes.Cast<XmlNode>().Single( x => x.Attributes.GetNamedItem( "name" ).Value.StartsWith( "{" ) );
+        }
+
+        public static IEnumerable<XmlNode> ObservationResults( this XmlNode assemblyNode )
+        {
+            return GetClassResult( assemblyNode, 0 ).ChildNodes.Cast<XmlNode>()
+                .SkipWhile( x => !IsObservationSetupNode( x ) ).Skip( 1 )
+                .TakeWhile( x => !IsObservationTeardownNode( x ) )
+                .Select( x => x );
+        }
+
+        private static bool IsObservationSetupNode( XmlNode x )
+        {
+            return x.Attributes.GetNamedItem( "name" ).Value.StartsWith( "{" );
+        }
+        private static bool IsObservationTeardownNode( XmlNode x )
+        {
+            return x.Attributes.GetNamedItem( "name" ).Value.StartsWith( "}" );
+        }
+        public static IEnumerable<XmlNode> AssertionResults( this XmlNode assemblyNode )
+        {
+            return GetClassResult( assemblyNode, 0 ).ChildNodes.Cast<XmlNode>().Except( assemblyNode.ObservationResults() );
+        }
+
+        public static XmlNode Second( this IEnumerable<XmlNode> source )
+        {
+            return source.ElementAt( 1 );
+        }
+        public static XmlNode First( this IEnumerable<XmlNode> source )
+        {
+            return source.First<XmlNode>();
+        }
+
+        public static void VerifyOutput( XmlNode testNode, string expectedOutput )
+        {
+            XmlNode outputNode = testNode.SelectSingleNode( "output" );
+
+            Assert.Equal( expectedOutput, outputNode.InnerXml.Trim() );
+        }
+
+        public static void VerifyPassed( XmlNode testNode )
+        {
+            ResultXmlUtility.AssertAttribute( testNode, "result", "Pass" );
+        }
+
+        public static void VerifyFailed( XmlNode testNode )
+        {
+            ResultXmlUtility.AssertAttribute( testNode, "result", "Fail" );
+        }
+
+        public static void VerifyFailedWith( XmlNode testNode, string expectedFailure )
+        {
+            VerifyFailed( testNode );
+
+            XmlNode firstFailureNode = testNode.SelectSingleNode( "failure" );
+
+            string innerxml = firstFailureNode.SelectSingleNode( "message" ).InnerXml;
+            Assert.True( innerxml.StartsWith( expectedFailure ), String.Format( "\nExpected: {0}\nActual: {1}", expectedFailure, innerxml ) );
+        }
+    }
+}