Could someone please tell me why I am not able to mock CriteriBuilder in my test. When criteriaBuilderMock.createMock() is executed I am getting Error casting map to grails.gorm.CriteriaBuilder, Reason: null
Is it possible to mock CriteriaBuilder? Am I getting this error because grails does not allow mocking objects without default constructor? Is there work around?
#TestFor(ProductService)
class ProductServiceSpec extends Specification {
void "test exists"() {
given:
def criteriaBuilderMock = mockFor(CriteriaBuilder);
criteriaBuilderMock.createMock(); //<-- error org.codehaus.groovy.runtime.typehandling.GroovyCastException: Error casting map to grails.gorm.CriteriaBuilder, Reason: null
}
}
thanks
The mocked class has to have a no-args constructor.
Related
Below code throws a runtime error.
TypeError: Instance of 'FormatException': type 'FormatException' is not a subtype of type 'CustomException'
Why Test(e) does not fail at compilation as type of e is Exception and expected is CustomException. How to enforce it so one cannot pass Exception there.
abstract class CustomException implements Exception {
String get code;
}
class Test {
final CustomException ex;
Test(this.ex);
}
void main() {
try {
throw new FormatException();
} on Exception catch (e) {
final t = Test(e);
print('message: $t');
}
}
Dart has (for now) implicit downcasts from a supertype to a subtype. You are allowed to use an expression which is a super-type of the actual type that is required, under the assumption that you know what you are doing.
Here you have a value with static type Exception (the e that was caught), and you pass it to a constructor requiring a CustomException, which is a subtype of Exception.
The language allows this, but inserts a run-time downcast (equivalent to e as CustomException). That cast fails because the value is actually a FormatException.
With the up-coming null safety feature, implicit downcasts will be removed except from dynamic (because dynamic turns off static type checks anyway). When that happens, the Test(e) invocation becomes invalid. Until then, this code compilers and fails at run-time.
Until then, you can get the analyzer to warn you about implicit calls by configuring it in the analysis_options.yaml file
I need to access the replaceTrack method of the RtcRtpSender, however, it is not defined in the dart2js file:
#Native("RTCRtpSender")
class RtcRtpSender extends Interceptor {
// To suppress missing implicit constructor warnings.
factory RtcRtpSender._() {
throw new UnsupportedError("Not supported");
}
MediaStreamTrack? get track native;
}
The replaceTrack method is defined here: https://developer.mozilla.org/en-US/docs/Web/API/RTCRtpSender/replaceTrack
How can I call the RtcRtpSender.replaceTrack()?
I've tried the below and it threw _DartObject method not found:
JsObject.fromBrowserObject(myRtcRtpSender).callMethod('replaceTrack',
[null]);
thanks to #sunbreak in gitter he reminded me of dart:js_util:
import 'package:js/js_util.dart' as js_util;
js_util.callMethod(myRtcRtpSender, 'replaceTrack',
[null]);
I have the defined following controller
class BookController {
def book(BookCommand bookCommand,
AnotherBookCommand bookCommand2)
{
....
}
When I debugged it, binding is done on the bookCommand and when it's bookCommand2's turn for binding, it throws this exception
Error in object 'bookCommand2': codes []; arguments []; default message [Error occurred initializing command object [bookCommand2]. org.apache.groovy.json.internal.Exceptions$JsonInternalException: Wrapped Exception
CAUSE java.io.IOException :: Stream closed]
If I try to switch the order of the parameters i.e.
class BookController {
def book(AnotherBookCommand bookCommand2,
BookCommand bookCommand)
{
....
}
Binding is done on the bookCommand2 and binding of bookCommand throws the exception.
Error in object 'bookCommand': codes []; arguments []; default message [Error occurred initializing command object [bookCommand]. org.apache.groovy.json.internal.Exceptions$JsonInternalException: Wrapped Exception
CAUSE java.io.IOException :: Stream closed]
Any idea what's happening here?
We do not support binding the request of the body to multiple command objects. One option you have is to aggregate them into 1 class with something like this:
class SomeClass {
BookCommand bookCommand
BookCommand anotherBookCommand
}
Then have your controller action accept one of those and organize the body of the JSON accordingly.
I'm new to Codeception, but I'm running into an issue injecting Page Objects. The problem occurs when I add the following construct logic to my page object.
public function __construct(\AcceptanceTester $I) {
$this->tester = $I;
}
... I got this from Login Page object example here: http://codeception.com/docs/06-ReusingTestCode#PageObjects
The error I'm getting is:
[Codeception\Exception\InjectionException]
Failed to inject dependencies in instance of 'MyCest'. Failed to create instance of 'Page\Login'. Failed to create instance of 'AcceptanceTester'. Failed to create instance of 'Codeception\Scenario'. Failed to resolve dependency 'Codeception\TestCase'.
This is how I'm injecting the page in my Cest.
protected function _inject(\Page\Login $login) {
$this->login_page = $login;
}
If I remove the __construct code, the error goes away. Is this a bug in Codeception or am I doing something wrong?
This is the work-around I found...
use \AcceptanceTester;
use Page\Login as LoginPage;
class MyCest {
protected $login_page;
public function _before(AcceptanceTester $I) {
$this->login_page = new LoginPage($I);
}
}
It is expected behaviour.
Your LoginPage constructor should not have any arguments to be instantiated during DI, so your workaround is right way to initialize LoginPage instance with AcceptanceTester instance.
When you specify AcceptanceTester as LoginPage's ctor arg DI mechanism of Codeception tries resolve dependencies recursively in the following way:
LoginPage(AcceptanceTester) -> AcceptanceTester(Scenario) -> Scenario(TestCase) -> TestCase
but TestCase is abstract class so it can not be instantiated.
I'm adapting the Clang tool-template (as described here) to search for a particular method call in my code. In order to later rewrite that call, I would like to get the type of the parameters the method was called with, as well as the type of the object the method was called on.
I managed to find a matcher that calls back the following:
class AddListenerPrinter : public MatchFinder::MatchCallback
{
public :
virtual void run(const MatchFinder::MatchResult &Result) {
if (const auto *FS = Result.Nodes.getNodeAs<clang::MemberExpr>("ListeningBound"))
{
FS->dump();
}
}
};
which prints out:
MemberExpr 0x7fb05b07b948 '<bound member function type>' .addListener 0x7fb05b077670
`-MemberExpr 0x7fb05b07b918 'class MyCore' lvalue ->mCore 0x7fb05b078e30
`-CXXThisExpr 0x7fb05b07b900 'class MyComponent *' this
Now I can't find any way to retrieve the type of the object the method was called on (here class MyCore) or the type of the method argument (here class MyComponent).
How can I do this?
I found the answer by browsing the code of the existing matchers.
Using matcher = memberCallExpr( callee(methodDecl(hasName("addListener"))) )
I was able to retrieve a CXXMemberCallExpr node. Then getting the type of the object the method was called on:
// FS is the CXXMemberCallExpr
// Prints out the type of x in x.method()
llvm::outs() << FS->getRecordDecl()->getName();
and the method parameters are accessible through FS->getArg(n).
Bottom line is: Find the CXX object that contains what you're looking for first (e.g. which class has methods to access function arguments?), then find the matcher that will return the same type of object in ASTMatchers.h.
Hoping this can help anybody else with the same problem.