Jasmine spyOn with CoffeeScript on Rails 3.1 with test_track - coffeescript

I'm using Jasmine with CoffeeScript (in Rails 3.1) and test_track. Everything else is fine, but I cant have spyOn on function calls to work:
describe "spyOn", ->
it "should spy", ->
foo =
bar: ->
spyOn(foo, 'bar')
foo.bar()
expect(foo.bar).toHaveBeenCalled()
the expect fails. What's wrong?
Update: the same exact code works on tryjasmine. It could be a bug with the version of jasmine with test_track.

For those who got the same problem, here's the answer:
https://github.com/quickleft/test_track/issues/7#issuecomment-2570580

Have a look at the compiled JavaScript and debug from there. Perhaps those returns are causing problems?
describe("spyOn", function() {
return it("should spy", function() {
var foo;
foo = {
bar: function() {}
};
spyOn(foo, 'bar');
foo.bar();
return expect(foo.bar).toHaveBeenCalled();
});
});

Related

Is there an efficient way to assert that all the elements of a PageObject are visible?

It's great how you can describe key elements of a page using the elements object in nightwatch's PageObject.
A very basic test of "is the page rendered correctly?" seems to consist of a string of asserts that every element is visible:
'Does it look right' : function (browser) {
browser.page.welcome()
.navigate()
.assert.title('My App')
.assert.visible('#header')
.assert.visible('#usernameField')
.assert.visible('#passwordField')
.assert.visible('#forgotPasswordLink')
.assert.visible('#signupButton')
Is there a more efficient way to do this?
You can run a script with your custom logic and then test against that
var script = function () {
//some logic or query selector test
return foo;
};
module.exports = {
'Test PCC': function (browser) {
var test;
browser
url('foo')
.waitForElementVisible('body', 1000)
.execute(script, [], function (response) {
console.log(response);
test = response.value;
})
//assert. test is something
.end();
}
};

Jasmine-node and sails.js (0.10.0-rc8)

When I try to run something as simple as this, I get an error: 'static() root path required'.
If only one 'it' is run, it will pass.
Anyone knows what's the catch?
var Sails = require('sails');
describe("Crud tests:", function() {
var app;
beforeEach(function(done) {
// Lift Sails and start the server
Sails.lift({
log: {
level: 'error'
},
}, function(err, sails) {
console.log("sails lifted");
app = sails;
done(err, sails);
});
});
afterEach(function(done) {
Sails.lower(done);
console.log('sails down');
});
it("1", function(done) {
expect(1).toEqual(1);
done();
});
it("2", function (done) {
expect(2).toEqual(2);
done();
});
});
See https://github.com/balderdashy/sails/issues/1860, quoted below:
Looking at the core tests, even in the ones where we lift / lower for
each individual test, it's always with a fresh instance of Sails. I
don't think a lot of testing has gone into lowering / re-lifting the
same instance, and I wouldn't be shocked to find out that some globals
are hanging around that screw up the lift sequence. So unless there's
a reason why you need it to be the same Sails, rather than a new Sails
with the same options, I'd follow the example of the core tests and
create a fresh instance. To do so, you require the Sails factory,
instead of the full Sails module:
var Sails = require('Sails/lib/app')
var sailsInstance = new Sails();
sailsInstance.lift(...);
I think that the sails v0.10 should be lifted differently. The code bellow is from my project which runs on rc9.
# test/support/sails.coffee
process.env.NODE_ENV = 'test'
process.env.PORT = 1338
Sails = require('sails/lib/app')
app = Sails()
beforeEach (done) ->
app.lift
models:
migrate: 'drop' # rebuild database (optional)
, done
afterEach (done) ->
app.lower done
describe ...
I hope it helps.

AngularJS Testing a Service with $http and $q

I'm new to AngularJS and to JS testing in general and I'm having trouble wrapping my mind around how to go about testing this rather simple service. I've tried using $httpBackend with 'when' and 'expect' GET in variations configurations, to no avail. The test should verify that 1) data is returned via the deferred.resolve and 2) no data is returned via the deferred.reject. If someone could point me in the right direction i'd be quite grateful. Thanks!
btw: I'm using Jasmine + Testacular
.service('myService', function($http, $q) {
return {
getMyData: function() {
var deferred = $q.defer();
$http.get('/foo/bar.do').success(function(data) {
deferred.resolve(data);
}).error(function(){
deferred.reject();
});
return deferred.promise;
}
}
})
I had kind of the same problem testing a service with $http and $q.
Here is one of my test which passes:
it('should issue a GET request to /foo/bar.do', inject(function ($httpBackend) {
$httpBackend.when('GET', '/foo/bar.do').respond('success');
var finalResult = '';
var result = myService.getMyData();
result.then(function(data) {
finalResult = data;
}, function() {
console.log('error');
});
$httpBackend.flush();
expect(finalResult).toBe('success');
}));

Coffeescript and jQuery chaining

I'm attempting this in coffeescript:
$( element ).mousedown( aFunction ).mouseup( anotherFunction );
I'm trying to work out a way to make use of indents so that something like the following will return what's about:
$ element
.mousedown aFunction
.mouseup anotherFunction
But to no avail, are there any recommendations for chaining in coffeescript?
I'm sure you don't want to use parenthesis, but...
$("#element")
.mousedown(aFunction)
.mouseup(anotherFunction)
Compiles to
$("#element").mousedown(aFunction).mouseup(anotherFunction);
For all the other quick readers out there, here's the updated answer by a paid nerd given here.
req = $.get('foo.html')
.success (response) ->
do_something()
.error (response) ->
do_something()
...compiles to:
var req;
req = $.get('foo.html').success(function(response) {
return do_something();
}).error(function(response) {
return do_something();
});
Looks like mus is too short suggested it in a comment above as well.

Custom PhoneGap Plugin (iOS) Function Issue

I'm using this tutorial to create a custom PhoneGap plugin:
http://wiki.phonegap.com/w/page/36753496/How%20to%20Create%20a%20PhoneGap%20Plugin%20for%20iOS
I have had success using the author's example, but I have a few questions that I have not been able to find out the answers to.
When the JavaScript function is created, the code is:
var MyPlugin = {
nativeFunction: function(types, success, fail) {
return PhoneGap.exec(success, fail, "PluginClass", "print", types);
}
};
Is there a way to set this up without var MyPlugin = {...}; and nativeFunction? In other words, can we define a function of our plugin like myfunc = function()...
Secondly, assuming there is a way to do the above, could this code:
MyPlugin.nativeFunction(
["HelloWorld"] ,
function(result) {
alert("Success : \r\n"+result);
},
function(error) {
alert("Error : \r\n"+error);
}
);
(which is the test code to test the plugin) also be written in a more standardized way? I.e., just a call to Javascript function without the nativeFunction part?
I would very much appreciate any input, thank you!
the phonegap documentation for plugins sucks. Honestly I had a bunch of issues when trying to create my own. A few tips :
the reason for doing
var MyPlugin = {};
is because this allows us to us scope things specific to that js object.
example:
MyPlugin.myFunction();
My favorite method to create plugins, similar to your question, is to prototype them
var MyPlugin = {}; // our object
MyPlugin.prototype.myFunction = function(success,fail,types){
}
The key to making a plugin fire is this -
PhoneGap.exec(success,fail,"MyPlugin","myFunction",types);
But something that they leave out is, what if we want to have options to our plugin? What if we want to do more than pass a string, then the example doesn't work. The fix is easy but not talked about at all.
var MyPlugin = {};
MyPlugin.prototype.myFunction = function(success,fail,options){
var defaults = {
foo: '', // these are options
bar: '',
};
// this parses our "options"
for(var key in defaults) {
if(typeof options[key] !== "undefined") defaults[key] = options[key];
}
return PhoneGap.exec(success,fail,"MyPlugin","myFunction",[defaults]);
}
when we call this with out javascript -
var foo = MyPlugin.myFunction(success,fail,{
foo:'hello',
bar:'world'
});
You'll notice that most of the phonegap API uses this syntax, which I found strange that their documentation didn't really talk about how to do this.
I have a post about a plugin I create you can check it out for reference.
Blog - http://www.drewdahlman.com/meusLabs/?p=138
Git - https://github.com/DrewDahlman/ImageFilter