Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unittest throwsXXX matchers don't compare to value in 'actual' #6761

Closed
DartBot opened this issue Nov 16, 2012 · 6 comments
Closed

Unittest throwsXXX matchers don't compare to value in 'actual' #6761

DartBot opened this issue Nov 16, 2012 · 6 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).

Comments

@DartBot
Copy link

DartBot commented Nov 16, 2012

This issue was originally filed by @butlermatt


In the past when we wanted to match if a piece of code would throw a specific error, we needed to wrap it in a function and assign it. Such as:

test('NoSuchMethodError', () {
  var silly = true;
  var broken = () => silly.doesntExist();

  expect(broken, throwsNoSuchMethodError);
});

However, recently (unfortunately I'm not sure exactly how long ago it was as I've not been able to look at this project in a little while) a change to unit test has meant that the 'actual' value is never checked and if an error/exception is thrown anywhere in a test, and we have a throws.... matcher then it will pass. No matter what. Take this code for example.

test('NoSuchMethodError', () {
  var silly = true;
  var broken = () => silly.doesntExist();

  expect(true, throwsNoSuchMethodError);
});

To me, this looks like we're saying 'true' throws NoSuchMethodError, even though it's actually detecting the throw from the anonymous function above within the test case.

My question is, is this intended behaviour to automatically catch a throw anywhere in the test case or a bug?

@DartBot
Copy link
Author

DartBot commented Nov 16, 2012

This comment was originally written by @butlermatt


Opps forgot to mention. I'm using Ubuntu Linux 64-bit, 64-bit dart editor:
Dart Editor version 0.2.3_r14669
Dart SDK version 0.2.3.0_r14669

@lrhn
Copy link
Member

lrhn commented Nov 16, 2012

It seems correct in this example.
The first argument to expect should be a function. Passing it a non-function means that it tries to call the non-existing "call" method on true, which will indeed throw a noSuchMethodError.

Is this happening in other cases too, or is it just NoSuchMethodError?
There have been changes recently that makes incorrect calls now throw NoSuchMethodError as the specification requires.


Added NeedsInfo label.

@DartBot
Copy link
Author

DartBot commented Nov 16, 2012

This comment was originally written by @butlermatt


Yes now that you mention it, it does appear to be only for NoSuchMethodErrors. My evaluation of the issue wasn't entirely correctly.

This still seems unexpected to me however, for my 'actual' I expect the code I place there to throw the error, not for the attempt to run that code (ie trying to call the 'call' method). I wouldn't expect "true" to throw NoSuchMethodError, and this test should fail:

test("This should fail", () {
  expect(true, throwsNoSuchMethodError);
});

@anders-sandholm
Copy link
Contributor

Added Area-Language label.

@gramster
Copy link
Contributor

gramster commented Feb 9, 2013

If I understand you correctly, what you are really saying is that if the actual is not an instance of Function, then a throwsX matcher should fail. Does that sound like a reasonable interpretation? That would be an easy change to make.

@gramster
Copy link
Contributor

Fixed in r18401


Added Fixed label.

@DartBot DartBot added Type-Defect area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Feb 12, 2013
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).
Projects
None yet
Development

No branches or pull requests

4 participants