-
Notifications
You must be signed in to change notification settings - Fork 100
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
error when mocking a Scala.js native method #191
Comments
Thanks, will take a look. Runtime says JVM, but your report is about the JS backend if i read the test case right? Also, there's ScalaMock 3.6.0 now, but doubt that would change anything, given the error message you reported. |
My bad, this is a Scala.js project so should have said JS, just fixed that. Thanks for looking at this. |
Added |
Could you try to create an adaptor between your code and the native class? E.g. like this:
Been playing around a bit, but I fear a fix may be shorlived when Scala-js moves to 1.0 and prevents overrides, so I am not sure if it is fixable from a library PoV. Will continue to play, but maybe the workaround is fine?
|
I am not familiar with mocking internals, but I am curious how Mockito solves this problem. Workaround makes sense, thanks @barkhorn. Given there's a workaround, that lowers the priority of this issue, although would certainly be nice if mocking classes like this worked out of the box. |
First, there is something fishy about the |
I do not really understand what I am not sure what to do about those :-s |
|
Ah OK. If it is anonymous, then it is automatically
If you're talking about this warning message:
Then the solution to it (both to make the warning disappear and make it 1.0-proof) is to replace the
I'm not sure I understand. Things have an abstract class/trait if and only if the library declares an abstract class/trait. Scala.js would not interfere with the library's decision. Are you talking about |
Thanks, makes sense not to introduce abstractions - just wanted to be sure there isn't an easy way out before investing a lot of time.. |
You don't have to inspect the trees for that. The body of a concrete method |
good to know, but still need to check parameters for the defaults (which need repeating)? |
For the defaults, if they are in an |
Noting for the record that I tried to implement the workaround discussed earlier, turns out to be not so easy. The idea proposed by @barkhorn (see above) is to create an adaptor The first problem is that we get a canvas in the first place by code like this: Now we have our hands on a CanvasRenderingContext2D, which doesn't implement the CanvasAdaptor trait. I think the answer is to have RealCanvas instead wrap an instance of CanvasRenderingContext2D: Next problem is that methods like this with js.native default arg values: Having done all that, the resulting code sort of kind of works, but there is some strange runtime behavior (e.g., getting a font string with an invalid size: 14.14.4px sans-serif) that I can't figure out. Likely I'm making some Scala.js noobie mistakes in the implementation, but for now I need to put this on hold in order to make progress. Thanks for everyone's efforts to figure out how to get the framework to handle this case properly! |
added a test case at https://github.com/paulbutcher/ScalaMock/blob/master/js/src/test/scala/org/scalamock/jstests/JSNativeTest.scala but wasn't able to develop a fix yet. |
If you want to discuss a new feature, please ignore/clear this form.
ScalaMock Version (e.g. 3.5.0)
3.5.0
Scala Version (e.g. 2.12)
2.11.8
Runtime (JVM or JS)
JS
Please describe the expected behavior of the issue
Mocking the CanvasRenderingContext2D class should yield a mock object
Please provide a description of what actually happens
Compilation failure:
Error:(18, 24) When overriding a native method with default arguments, the overriding method must explicitly repeat the default arguments.
val canvas = mock[CanvasRenderingContext2D]
Reproducible Test Case
Here's a really simple test case that won't compile (yields the error shown above, with different row/col values because not the same code as used above). Note that this test case works fine with Mockito, so I'll switch to Mockito for now, but would love to be able to use ScalaMock.
The text was updated successfully, but these errors were encountered: