Comments (15)
Oh wow! @CaptnCodr thatโs awesome! Iโll try it out.
from fsunit.
Hey @abelbraaksma,
thanks for the little repro, I'll look at it this week. ๐
from fsunit.
you don't need ignore anymore as in your opening post:
exactly, butโฆ
Also on functions that return obj or a generic type 'a. That is valid for throwWithMessage.
this didnโt work
tested it with throw without ignore, that test failed for xUnit. I'll put it on my list (#225).
Awesome, tx!
from fsunit.
Thank you @abelbraaksma for pointing out. ๐
@sergey-tihon How do we proceed with this issue?
Maybe taking an additional note as comment at line 92-93 in the docs?
(fun () -> failwith "BOOM!" |> ignore) |> should throw typeof<System.Exception> // |> ignore is important here.
(fun () -> failwith "BOOM!" |> ignore) |> should (throwWithMessage "BOOM!") typeof<System.Exception> // |> ignore is important here aswell.
Any other suggestions or smth else?
from fsunit.
Isn't it possible to just catch & verify the exception? The return type shouldn't be a problem, right? It would alleviate the subtle requirement of using ignore
and as such behave more like Assert.Throws
(or whatever the xUnit method is).
from fsunit.
We have 2 options
- Support functions that return
obj
and merge #220 - Fail in runtime and ask to refactor
testFunc
to returnunit
I have no idea why unit -> obj
was not supported before, it was way before me ๐ง
from fsunit.
Hello @abelbraaksma,
a fix (#220) has been released in v5.0.5 and is already published on Nuget. ๐
ignore
is not necessary anymore. ๐
from fsunit.
@CaptnCodr, are you sure it is part of 5.0.5? I just double-checked that I have that version, but I get the following:
FsUnit.Xunit+MatchException : Exception of type 'FsUnit.Xunit+MatchException' was thrown.
Expected: System.Text.Json.JsonException "Expected a string, but given a Json Token Type of Number for 'amount'."
Actual: <fun:Pipe #1 input at line 29@29>
The function looked like this, which is then called from the tests:
let shouldThrowWith msg data =
fun () -> data |> deserialize<Money>
|> should (throwWithMessage msg) typeof<JsonException>
It works with ignore
.
Did I hit a fringe case or is it possible that the fix didn't make it to 5.0.5?
from fsunit.
are you sure it is part of 5.0.5?
Yes, definitely! I downloaded the nuget package and decompiled the library and the change is in version 5.0.5.
May you try to capsulate the actual
part with paranthesis? Like this:
let shouldThrowWith msg data =
(fun () -> data |> deserialize<Money>)
|> should (throwWithMessage msg) typeof<JsonException>
or did you try that already?
from fsunit.
I'll try to make a minimal repro, I'll get back to this.
from fsunit.
Hey @abelbraaksma, is here any progress?
Let me know when I can help you.
from fsunit.
@CaptnCodr, thanks for pinging me. Sorry, I kinda forgot about this but now, cleaning up my messages, I'm back ;).
Using your latest version, this is a simple example of a function that does throw, but does not successfully pass (adopted from a similar function in my TaskSeq
library, where I just hit this issue again recently):
// minimal repro:
[<Fact>]
let ``Null source is invalid`` () =
let test1 () = Seq.empty |> Seq.append null
test1 |> should throw typeof<ArgumentNullException>
This gives the follow (wrong) error:
Message:โ
FsUnit.Xunit+MatchException : Exception of type 'FsUnit.Xunit+MatchException' was thrown.
Expected: System.ArgumentNullException
Actual: <fun:test1@28T>
Changing this by adding ignore
as before, the test succeeds:
[<Fact>]
let ``Null source is invalid`` () =
let test1 () = Seq.empty |> Seq.append null |> ignore
test1 |> should throw typeof<ArgumentNullException>
What I currently use as a workaround is just a simple helper:
let inline assertThrows ty (f: unit -> 'U) = f >> ignore |> should throw ty
let inline assertNullArg f = assertThrows typeof<ArgumentNullException> f
from fsunit.
Hey @abelbraaksma,
ignore
is definately needed here, the signature needs to be (unit -> unit)
. This is the intended way.
So, I would not implement something for your concern here.
Thank you.
from fsunit.
Well, I appreciate it, assuming this isnโt as simple to fix as I thought. Though you did write a few messages before:
ignore
is not necessary anymore.
And I have seen some functions not needing it. @CaptnCodr do you know in what cases or why it is still needed sometimes?
from fsunit.
Hey @abelbraaksma,
when you throw an exception, you don't need ignore
anymore as in your opening post:
(fun() -> raise(ApplicationException "BOOM!"))
|> should (throwWithMessage "BOOM!") typeof<ApplicationException>
Also on functions that return obj
or a generic type 'a
.
That is valid for throwWithMessage
.
I tested it with throw
without ignore
, that test failed for xUnit. I'll put it on my list (#225).
from fsunit.
Related Issues (20)
- ofCase doesn't handle None (represented as null) HOT 3
- equalWithin on DU types of float HOT 6
- Implementing Expecto HOT 6
- `result |> should be (ofCase <@ Ok @>)` throws exception at runtime HOT 3
- Error message when DateTime is not equal does not provide any help HOT 4
- Error message for "equal" is difficult to read and does not show the date milliseconds also when that is the only difference
- Method not found: 'Void Xunit.Sdk.AssertActualExpectedException..ctor HOT 2
- Pipelines are not correctly failing HOT 3
- Inconsistent behaviour of `should equal` in NUnit vs XUnit HOT 1
- Update docs site HOT 7
- error FS0074: The type referenced through 'NHamcrest.IMatcher`1' is defined in an assembly that is not referenced. HOT 2
- Using `should throw` does not show the raised exception, only the string version of the lambda HOT 5
- [Feature request] Creating custom constraints HOT 4
- Deep equality for C# types HOT 5
- Comparing sequences fails HOT 13
- New Year's ToDos HOT 1
- Is FSharp.Core 6.0.7 really the lowest dependency for FsUnit? HOT 3
- `MatchException` does not exist anymore in 2.5.0 HOT 2
- Do not use an upper bound on dependencies, unless you have to HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fsunit.