Commits

ptrelford committed 9c23cf3

Added tests for Stub type.
By default methods of a stub type that return nothing do not throw when called.

  • Participants
  • Parent commits 65ec443

Comments (0)

Files changed (6)

                 gen.Emit(OpCodes.Unbox_Any, mi.ReturnType)
                 gen.Emit(OpCodes.Ret)
             | None ->
-                let ctor = typeof<NotImplementedException>.GetConstructor([||])
-                gen.Emit(OpCodes.Newobj, ctor)
-                gen.Emit(OpCodes.Throw)
+                if abstractMethod.ReturnType = typeof<System.Void> then
+                    gen.Emit(OpCodes.Ret)
+                else
+                    let ctor = typeof<NotImplementedException>.GetConstructor([||])
+                    gen.Emit(OpCodes.Newobj, ctor)
+                    gen.Emit(OpCodes.Throw)
             if abstractType.IsInterface then 
                 typeBuilder.DefineMethodOverride(methodBuilder, abstractMethod)
 
 [<AutoOpen>]
 module It =
     let IsAny<'a> = obj() :?> 'a
-    let inline any () : 'a = IsAny()
+    let inline any () : 'a = IsAny()
+
+module Test =
+    type IFock =
+        abstract Insert : double * unit -> int
+        abstract DoNothing : unit -> unit
+
+    let stub = 
+        Stub<IFock>()
+            .Method(fun x -> <@ x.Insert(any(),any()) @>).Returns(2)
+    let instance = stub.Create()
+    let returnValue = instance.Insert(2.0,())
+    do instance.DoNothing()
     <Reference Include="FSharp.Core" />
     <Reference Include="System" />
     <Reference Include="System.Core" />
-    <Reference Include="System.Numerics" />
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Fock.fs" />

File Tests/StubTests.fs

+module Fock.Tests
+
+open Fock
+open NUnit.Framework
+
+type IFock =
+    abstract Insert : double * unit -> int
+    abstract DoNothing : unit -> unit
+    abstract Name : string
+
+[<Test>]
+let ``an interface method that is not implemented that returns something should throw`` () =
+    let stub = Stub<IFock>().Create()
+    Assert.Throws<System.NotImplementedException>( fun () -> stub.Insert(1.0,()) |> ignore ) |> ignore
+
+[<Test>]
+let ``an interface method that is not implemented and returns nothing should not throw`` () =
+    let stub = Stub<IFock>().Create()
+    Assert.DoesNotThrow( fun () -> stub.DoNothing() )
+
+[<Test>]
+let ``an implemented interface method should return the specified value`` () =
+    let stub = 
+        Stub<IFock>()
+            .Method(fun x -> <@ x.Insert(any(),any()) @>).Returns(2)
+            .Create()
+    let returnValue = stub.Insert(2.0,())
+    Assert.AreEqual(returnValue,2)
+
+[<Test>]
+let ``an implemented interface property getter should return the specified value`` () =
+    let stub = 
+        Stub<IFock>()
+            .Method(fun x -> <@ x.Name @>).Returns("Fock")
+            .Create()
+    let returnValue = stub.Name    
+    Assert.AreEqual(returnValue,"Fock")
+

File Tests/Tests.fsproj

+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>8.0.30703</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{48c44b15-2de0-4990-a208-dae2f59a5052}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <RootNamespace>Fock.Tests</RootNamespace>
+    <AssemblyName>Fock.Tests</AssemblyName>
+    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <Name>Fock.Tests</Name>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <Tailcalls>false</Tailcalls>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <WarningLevel>3</WarningLevel>
+    <DocumentationFile>bin\Debug\Fock.Tests.XML</DocumentationFile>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <Tailcalls>true</Tailcalls>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <WarningLevel>3</WarningLevel>
+    <DocumentationFile>bin\Release\Fock.Tests.XML</DocumentationFile>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="mscorlib" />
+    <Reference Include="FSharp.Core" />
+    <Reference Include="nunit.framework">
+      <HintPath>nunit.framework.dll</HintPath>
+    </Reference>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="StubTests.fs" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\..\fock\Fock.fsproj">
+      <Name>Fock</Name>
+      <Project>{f620ddef-c762-4319-b470-91e7bf051cba}</Project>
+      <Private>True</Private>
+    </ProjectReference>
+  </ItemGroup>
+  <Import Project="$(MSBuildExtensionsPath32)\FSharp\1.0\Microsoft.FSharp.Targets" Condition="!Exists('$(MSBuildBinPath)\Microsoft.Build.Tasks.v4.0.dll')" />
+  <Import Project="$(MSBuildExtensionsPath32)\..\Microsoft F#\v4.0\Microsoft.FSharp.Targets" Condition=" Exists('$(MSBuildBinPath)\Microsoft.Build.Tasks.v4.0.dll')" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+	     Other similar extension points exist, see Microsoft.Common.targets.
+	<Target Name="BeforeBuild">
+	</Target>
+	<Target Name="AfterBuild">
+	</Target>
+	-->
+</Project>

File Tests/nunit.framework.dll

Binary file added.
 
 type IFock =
     abstract Insert : double * unit -> int
+    abstract DoNothing : unit -> unit
 
 let stub = 
     Stub<IFock>()
         .Method(fun x -> <@ x.Insert(any(),any()) @>).Returns(2)
 let instance = stub.Create()
-let returnValue = instance.Insert(2.0,())
+let returnValue = instance.Insert(2.0,())
+do instance.DoNothing()