Search code examples
exceptiontypesf#xunit.nettestdriven.net

F# Unit Test Error: xUnit.net / TestDriven.Net


So the following I got from Pluralsight and the chap presenting the course apparently runs the code without a hitch, however, when I run it, I get the error message:

"Test failed ‘T:Messaging’ : Couldn’t find type with name ‘Messaging’"

Test failed 'T: Couldn't find type with name

Any Ideas (appreciated)?

namespace Ploeh.Samples

open System

module Messaging =    
    type Envelope<'a> = {
        Id      : Guid
        Created : DateTimeOffset
        Item    : 'a }

    let envelop getId getTime item = {
        Id = Guid "1CF889F8-201F-44DF-BC86-77227651D3EE"
        Created = DateTimeOffset.MinValue
        Item = item }

module MessagingTests =
    open Xunit

    type Foo = { Text : string; Number : int }

    [<Fact>]
    let ``enevelope returns correct results`` () =
        let getId _ = Guid "1CF889F8-201F-44DF-BC86-77227651D3EE"
        let getTime _ = DateTimeOffset( 636322011751405346L, 
                                        TimeSpan.FromHours(-4.0) )
        let item = { Text = "Bar"; Number = 42 }

        let actual = Messaging.envelop getId getTime item

        Assert.Equal ( Guid "1CF889F8-201F-44DF-BC86-77227651D3EE",
                       actual.Id )
        Assert.Equal ( DateTimeOffset( 636322011751405346L, 
                                       TimeSpan.FromHours(-4.0) ),
                       actual.Created )
        Assert.Equal ( item, actual.Item )  

Solution

  • I managed to get it working by separating the tests into a separate file and top-level module. For some reason, anything other than this `setup' does not work, e.g., separate file but in a lower-level module, or namespace and module declarations separately ... I am not sure whether this is something specific to say my target being .Net 4.7 rather than whatever used by the instructor or an issue with Visual Studio 2017, or something else. It seems that TestDriven.Net takes issue with the namespace/module system and gets confused by them ...

    In the first file:

    namespace Ploeh.Samples
    
    open System
    
    module Messaging =    
        type Envelope<'a> = {
            Id      : Guid
            Created : DateTimeOffset
            Item    : 'a }
    
        let envelop getId getTime item = {
            Id = Guid "1CF889F8-201F-44DF-BC86-77227651D3EE"
            Created = DateTimeOffset.MinValue
            Item = item }
    

    And in the second file:

    module Ploeh.Samples.MessagingTests 
    
    open System
    open Messaging
    open Xunit
    
    type Foo = { Text : string; Number : int }
    
    [<Fact>]
    let ``enevelope returns correct results`` () =
    let getId _ = Guid "1CF889F8-201F-44DF-BC86-77227651D3EE"
    let getTime _ = DateTimeOffset( 636322011751405346L, 
                                    TimeSpan.FromHours(-4.0) )
    let item = { Text = "Bar"; Number = 42 }
    
    let actual = Messaging.envelop getId getTime item
    
    Assert.Equal ( Guid "1CF889F8-201F-44DF-BC86-77227651D3EE",
                   actual.Id )
    Assert.Equal ( DateTimeOffset( 636322011751405346L, 
                                   TimeSpan.FromHours(-4.0) ),
                   actual.Created )
    Assert.Equal ( item, actual.Item )