CakePHP Unit Test Common Custom Assert - unit-testing

This is probably a really easy one, but I can't seem to find the answer.
I have written some custom assert functions for unit testing my models. I would like these functions to be available in all of my unit tests, no matter the model. Where would I put these so that they are available to every unit test?

I use MyCakeTestCase which extends the CakeTestCase.
There I can put all my custom methods.
see this test for example:
https://github.com/dereuromark/tools/blob/2.0/Test/Case/Lib/CaptchaLibTest.php
just put your own test case in your lib folder (either app or - as I prefer - plugin):
/app/Lib/
/app/Plugin/PluginName/Lib/
and either lib or pluginlib uses statement:
App::uses('MyCakeTestCase', 'Lib');
App::uses('MyCakeTestCase', 'PluginName.Lib');

Related

Jest: How to ignore __mocks__ folder for specific test and instead use a mock I define in the test file?

I have a __mocks__ folder that mocks out a node module. This works for most of my tests, but in one particular test I need a custom mock. I need my unit-tested code to ignore the mock in the __mocks__ folder, and use a specific mock that I define in the test file.
I tried using jest.unmock(), however this prevents me from defining specific mocks in my unit test (thing.test.js). If I then add some mocks or modify the module I'm mocking, the changes don't get added to the code I'm testing (thing.js).
Example:
thing.js imports AWS.js module
__mocks__/AWS.js contains AWS.js module mock
other tests use __mocks__/AWS.js
thing.test.js wants to create a custom mock that doesn't get overwritten by __mocks__/AWS.js and doesn't affect other tests -> how to do this??
I am using Typescript, but the same approach applies. I create my normal test files (thing.spec.ts) which tests the code. In our code base, we do basic tests in this file, and would use it to test non-mocked functions, and simply spyOn() calls.
We then create a separate test file (thing.mock.spec.ts) where the 'mock' indicates that the tests in the this file, are going to be using the __mock__ directory class instead. The naming is just our internal standard to be clear of what we are using.
In the thing.mock.spec.ts we do the mock of the complete class as you are doing in your test. This test file only tests functions that require the mock data, since the main tests have been done independently in the thing.spec.ts.
This would then have:
__mocks__/AWS.js
thing.js
thing.test.js
thing.mock.test.js
This way, when looking at just the file names, you get a sense of what is being used during the testing.

How to test React Native classes that do not have a render method?

I am building a mobile application with React Native and ran into a problem when trying to unit test my code. Currently I do have both Jest and Enzyme being used in the testing.
How would I be able to test the methods in a certain class/file that does not contain a render() method and does not "extend as a Component"? This class is used to query information from an API and saving it into variables.
The documentation about Jest and Enzyme at
https://facebook.github.io/jest/docs/api.html#content
http://airbnb.io/enzyme/docs/api/index.html
seem to be focused on testing rendered components.
Assuming you've got a module export set up, you actually just import the object/class and then write a test against it using a jasmine syntax. You can try it out live right here:
https://facebook.github.io/jest/docs/getting-started.html#content
If you notice on the "add-test.js" file, the first line is a require to get your object under test. In this case:
const add = require('./add');
Then it's just a matter of regular old jasmine-style testing.

CanJS: Unit testing of different parts

I have one question. I've started to use CanJS just recently and trying to create unit tests (funcunit / jasmine ) that will work in maven build with TeamCity (headless).
It was relatively easy to test Model, because it doesn't rely on any view and you can create instance and test functionality.
But it not so clear for me how to test Components and other parts of CanJS. Just to clarify i don't need E2E tests with user interaction, what i'm trying to achieve is just have some data provided by Can.fixtures and then just test that my functions works fine by calling them in tests.
Controller tests benefit from the addition of jasmine-fixture to your Jasmine test bed. You can affix() the appropriate DOM elements and instantiate the controller in beforeEach() before adding spies to the instance or the controller prototype, then fire events or directly call functions requiring some DOM tree to be available.
For Components, there's one more step involved. Because of the way Components are instantiated, either you have to use can.view() to create and attach their custom elements to the DOM (and clean it up in afterEach()), or you have to use can.view.callbacks.tagHandler(el, tag_name) to manually instantiate the Component for an element already in the DOM.
To be clear, this makes things easier when your controller/component functions are slurping data from the DOM, as in event handlers. It also works to just call the functions directly on the prototype and make spy objects for this.scope and this.options

Unit and integration tests get the same names by default

In Grails the unit tests and integration tests get generated with exactly the same naming convention, so that if you are testing a domain class named Foo both tests get generated with the name FooTests, in the same package. Is there an expectation I should have either a unit test or an integration test, but not both? Do people put their integration tests in a different package from their unit tests, or rename one? What's the preferred way to get around this?
btw I hacked on grails/scripts/_GrailsCreateArtifacts.groovy to change how the classes are generated in order to get it to generate different names (the value for the suffix property is all that changed). I don't like having to include something that means 'integration' in the name when the class is in a folder called 'integration', but this at least avoids having to do a manual rename.
createIntegrationTest = { Map args = [:] ->
def superClass = args["superClass"] ?: "GroovyTestCase"
createArtifact(name: args["name"], suffix: "${args['suffix']}IntTests",
type: "Tests", path: "test/integration", superClass: superClass)
}
I think giving them a different name is the right choice.
*IntTests
or
*IntegrationTests

Correct way to metaprogram in grails so its available in unit tests

I can add a method to the java Integer type in Groovy with the lines:
ExpandoMetaClass.EnableGlobally()
Integer.metaClass.gimmeAP = {->return 'p'}
I don't know why I'd need that, but it gets the point across. Now I can make calls to Integers and get back a 'p'. Now lets say I want this in a grails app so I can make calls in the domain objects. The specific problem I'm having is that when I put those metaprogramming lines in the bootstrap all the metaprogramming isn't available in the unit tests, so my unit tests are failing with errors like "No method gimmeAP for java.lang.Integer" or something like that.
How do I either include the metaprogramming better, or execute that part of the bootstrap so I can use my tricked out syntax in unit tests?
I have seen this question: Grails - Making Methods Globally Available and Metaclass Programming and it seems my line ExpandoMetaClass.EnableGlobally() may fix his problem, but am I using it right?
Bootstrap isn't executed for unit tests. I would personally prefer to create a mockFoo method that does the above meta programming, and Then I will call the mockFoo from the test setup.
Also look at the GrailsUnitTestCase.registerMetaClass. Register metaclass before you add mock methods, so that they don't leak in other tests.
registerMetaClass(SecurityUtils)
SecurityUtils.metaClass.'static'.getSubject = { ->
return [logout: { return true } ] as Subject
}
I know you want to make your dynamic methods available to all unit tests, but there's nothing such as bootstrap for unit tests. So you have to do it in each test.
You can create a MockHelper with a static method, and call it from test setUp.