GWT unit test for Activity and View - unit-testing

Does anybody have a link for a tutorial on how to write JRE junit tests (extending TestCase and not GWTTestCase) that tests Activity and Views in GWT 2.1?
Best regards
Pich

Views can only be unit tested using GWTTestCase because they call (either explicitly or implicitly) GWT.create().
To test Activities use mock Views to avoid use of GWT.create().

I have managed to test views, which of course contains reference to the objects that calls GWT.create(), using PowerMock.
For Activities it is easy todo using Mockito for instance to mock the View.

Related

How do you structure your Ember app with unit tests in mind?

I haven't written unit testing with JS before and I'm still learning ember and javascript in general, so forgive the question for its simplicity.
Ember apps are structured by first creating a namespace for the app like so:
MyApp = Ember.Application.create({...})
Then you'd define a model like so:
MyApp.SomeModel = DS.Model.extend({...})
But when I'm writing a unit test for SomeModel, it doesn't seem right to instantiate an Ember.Application instance when you're just unit testing a model, especially with all the Router injections and such going on behind the scenes.
However, to define the model you need the app namespace, MyApp.SomeModel .. and that app name space is created when the Application is instantiated (if you follow the Ember namespace convention).
So how do you unit test a model without instantiating the app? Do you create a mock for the app namespace?
I'm using jasmine btw.
I create a dummy instance of Ember.Application. It's not particularly costly to do.

Testing Mongoose Node.JS app

I'm trying to write unit tests for parts of my Node app. I'm using Mongoose for my ORM.
I've searched a bunch for how to do testing with Mongoose and Node but not come with anything. The solutions/frameworks all seem to be full-stack or make no mention of mocking stuff.
Is there a way I can mock my Mongoose DB so I can return static data in my tests? I'd rather not have to set up a test DB and fill it with data for every unit test.
Has anyone else encountered this?
I too went looking for answers, and ended up here. This is what I did:
I started off using mockery to mock out the module that my models were in. An then creating my own mock module with each model hanging off it as a property. These properties wrapped the real models (so that child properties exist for the code under test). And then I override the methods I want to manipulate for the test like save. This had the advantage of mockery being able to undo the mocking.
but...
I don't really care enough about undoing the mocking to write wrapper properties for every model. So now I just require my module and override the functions I want to manipulate. I will probably run tests in separate processes if it becomes an issue.
In the arrange part of my tests:
// mock out database saves
var db = require("../../schema");
db.Model1.prototype.save = function(callback) {
console.log("in the mock");
callback();
};
db.Model2.prototype.save = function(callback) {
console.log("in the mock");
callback("mock staged an error for testing purposes");
};
I solved this by structuring my code a little. I'm keeping all my mongoose-related stuff in separate classes with APIs like "save", "find", "delete" and no other class does direct access to the database. Then I simply mock those in tests that rely on data.
I did something similar with the actual objects that are returned. For every model I have in mongoose, I have a corresponding class that wraps it and provides access-methods to fields. Those are also easily mocked.
Also worth mentioning:
mockgoose - In-memory DB that mocks Mongoose, for testing purposes.
monckoose - Similar, but takes a different approach (Implements a fake driver). Monckoose seems to be unpublished as of March 2015.

Grails - How to Unit Test addTo*

Is it possible to unit test addTo* functions in Grails ?
thanks for your help.
The documentation says in section 9.1:
In Grails you need to be particularity
aware of the difference between unit
and integration tests because in unit
tests Grails does not inject any of
the dynamic methods present during
integration tests and at runtime.
You either have to use mockDomain(DomainClassName) in a unit test
or write an integration test:
Grails decorates domain object with some Dynamic methods when the DomainClassGrailsPlugin gets setup(doWithDynamicMethods).
I hit this problem in upgrading from Grails 2.1.2 to Grails 2.3.x. Where as before you only needed to mock the domain class you are adding to, now you need to also mock the domain class being added. Simple with Annotations.
#TestFor(YourService)
#Mock([MyClass, MyOtherClass])
class YourServiceTests {
.... //now myClass.addToMyOtherClasses(myOtherClassInstance) should work fine in your test or in the code being tested
}

Mocking a datacontext for an object that has a dependency

I'm writing some unit tests in my project and I have a datacontext dependency on the controller containing the methods I'd like to test.
I'm using Ninject to inject the dependency and Moq to create my mock datacontext. My DI makes use of an interface IDataContext which my dbml impliments and is used through out the injection process.
In my unit test I'm creating my mock datacontext as follows:
var mock = new Mock<IDataContext>();
var myController = new MyController(mock.Object);
This throws a Object reference not set to an instance of an object. exception on the second line whilst executing the datacontexts constructor.
I'm clearly missing a fundamental piece in setting this up however most of the Moq examples I've seen involve some kind of test against the mocked object using Setup().
Am I going about this the right way? Should I be creating a mock of my IDataContext interface or something else?
haha,
the answer came while i was reading through emad's blog on unit testing in ASP.Net MVC.
I'm guessing that you didn't add the connection string to the app.config of your test project right? :)
And that's the database dependency way because you're still not mocking the database end.
So if you want to do that, you need to google up for some codes, there are a few ways to do that.
i find these few references below to be quite useful, but since i don't really have a need to mock the database end, i'm just using my test DB server for now.
link

Mock ASP.Net User in WebForms app

I have a legacy app that I am trying to build some testing into.
I have some tests that require the HttpContext.Current.User
Is there a way to mock this, or simply login a user on the fly?
HttpContext.User is of type IPrincipal. You can mock it by assigning to it any IPrincipal object, including any CustomPrincipal object.
I don't now how much you can refactor your legacy application. If you have .NET 3.5, you can use HttpContextWrapper class that is part of System.Web.Abstraction assembly.
You can read more and see an example how to Stub/Mock HttpContext.