Karma with Mocha, Chai, Chai-as-promised and chai-sinon - karma-runner

I'm trying to get my Karma test suite running with Mocha, Chai, Sinon, Chai-as-promised and chai-sinon.
It's currently set up with just mocha, chai and sinon framework.
frameworks: ['mocha', 'requirejs', 'sinon', 'chai']
Now I've tried adding some additional karma plugins, namely the karma-chai-sinon and karma-chai-as-promised. Then changing the above to
frameworks: ['mocha', 'requirejs', 'chai-sinon', 'chai-as-promised', 'chai']
In my unit test I have
expect(stub).to.not.have.been.called();
but this gives me an error 'TypeError: '[object Object]' is not a function...'
But if I do
expect(stub.called).to.equal(false);
It works as expected.
I've tried inspecting the karma plugin code but can't make any sense of them, I expected somewhere in the karma plugin to say 'chai.use(chaiAsPromised)' and 'chai.use(sinonChai)' but they don't.
Karma website doesn't really explain how plugin/frameworks work under the hood either.
Can anybody help me set this up?
Thanks in advance.

By default, any assertions that don't take arguments have to be declared without brackets:
expect(stub).not.to.have.been.called;
(Yeah it's weird.)
That's because in this case called is a getter function which actually sets up the assertion when it's accessed.
If you want to call these assertions as functions using parentheses you can use a plugin like dirty-chai (and karma-dirty-chai).

Related

Pytest hooks flow

I'm new to pytest and I can't find solution for my question.
I know about some pytest's hooks like pytest_addoption() and pytest_configure()
I'm trying to figure if and how pytest_addoption() runs first and only then pytest_configure().
If anyone know where I can find some information or can share his knowledge.
Thanks!
The flow of the hooks in pytest are specified here:
https://pytest.readthedocs.io/en/2.8.7/_modules/_pytest/hookspec.html
see first line in the docstring below
def pytest_configure(config):
""" called after command line options have been parsed
and all plugins and initial conftest files been loaded.
This hook is called for every plugin.
"""

Getting "too many arguments for method apply" routes with multiple parameters

I have a routes file like this:
GET /getOf/:city/:fi/:state/:zipCode cont.Offer.getOf(city:String, fi:String, state:String, zipCode:String)
In my scala class, my code is like this:
def getOf(city:String, fi:String,state:String,zipCode:String) = Action(parse.anyContent) {request =>
val offer = Offer(city,fi,state,zipCode);
Ok(Json.toJson(offerService.getOffer(offer)));
}
But when I run I get this compilation error:
too many arguments for method apply: (name: String, constraint: String)play.core.DynamicPart in object DynamicPart
But same code works fine if I have only one argument. I even not understanding what's the problem. Yes, I have created Eclipse project with play clean-all and others. But still same problem persists.
Can anyone please guide me on this? As I am very new to Play framework and scala.
i got the same problem when i walk through from the below url http://scala-ide.org/docs/tutorials/play/index.html but the issue resolved by calling clean and then compile from play console solved the issue.
Note* i initially had the site at different folder.
I got this error only in eclipse IDE and if i make any changes to the views it compiled atonce whereas for controllers it check only when "RELOAD" works...till then you may see the error at eclipse ide but app works fine.

Way to obtain the list of test methods from the excluded group

My setup is like below:
I run my TestNG test with excludedGroups Maven Surefire Plugin parameter set to failing. So, test methods which are known to be failing are excluded from the test suite.
I want to obtain the list of those test methods.
I did not find a straightforward solution for this.
Does anyone know how to do it? Whether it can be done using capabilities of Java, Annotations, TestNG or Maven...
Couple of ways : Implement Isuitelistener->onStart method. Use the suite.getExcludedMethods to get a list of all excluded methods calculated. You can implement ITestListener as well and use context.getExcludedMethods/Groups for the list too.

Fay: include another Fay file?

I have one Fay file which is the heart of my program, however I need some helpers for my logic, for instance a method to replace substrings. From what I understand, if I need such methods which are offered by many Haskell libraries from Hackage directly, I can't use those Haskell libraries, but I must copy-paste the code in my project. So it's what I did, I copy-pasted a "replace" function together with other helpers from the MissingH library in a new file in my project: Utils.hs.
That Utils.hs compiles without problems with Fay. Also I import it in my main Fay file and I get a JS file for the main project file without problems. However at runtime I get the following error:
ReferenceError: Utils$$36$ is not defined
I don't think that Fay will include the code from the helper file in my main JS file, so I'm including both JS files in the loading HTML. And to make even more sure that when I load the main file, that the utils file is loaded, I load it like that:
$.getScript("Utils.js", function(){
$.getScript("FayConfig.js");
});
But despite this I still get the error. I tried compiling the Utils.hs with "--library" but it didn't help.
So my question is, which setup do I need to achieve that the generated JS will find the helper functions that I put in another HS file, knowing that at compile-time, Fay (apparently) finds them without problems? Is there an example of such a setup online? Most of the Fay uses that I found have all the code in a single HS file, though they often use external Fay code from cabal, as with fay-jquery. In my case, setting up a cabal project just for these simple helpers would be overkill.
Which version of Fay are you using (fay --version)? It seems like you are using a version older than
0.16 where forgetting import Prelude wouldn't give any warnings, see this closed ticket. So upgrade fay and/or add import Prelude.
We're also considering renaming operators in the produced output to make error messages like these easier to understand.
You do not need to invoke fay several times, fay outputs all dependencies into the same js file. So there's no difference from using a cabal package in that regard.
Hope this helps, otherwise please give me a way to reproduce this.

Don't execute certain code while running a JUnit Test in Eclipse

I'm using Eclipse and JUnit 4 while developing an application within a tomcat container. The container manages the connection to our Oracle database.
While testing with JUnit i've got the following problem: In the constructor of the test subject there is something like this:
public Subject() {
// stuff
FancySingleton.getInstance().getFancy("stuff");
}
Unfortunately the method getFancy() tries to execute a Query which it can't because JUnit does not run within the tomcat container and ends up in an endless loop.
My first idea was to out-commend the code. At second thought it appeared to be a bad idea. I could forget to remove the comments before committing.
My second idea was to highlight the code for eclipse so that it doesn't execute it while running a JUnit test. But it requires eclipse to support such a method.
At last i thought of something like preprocessor directives.
What is your idea? Just passing in a boolean to the constructor is imho not a clean way of dealing with such a circumstance.
You'd either mock FancySingleton, or you'd do it right and inject an implementation.