res.should.have.status gives me error - javascript

I'm new to mocha and should.js. I'm trying to check the response's status but it gives me TypeError: Object #<Assertion> has no method 'status' The code is like this:
describe('Local signup', function() {
it('should return error trying to save duplicate username', function(done) {
var profile = {
email: 'abcd#abcd.com',
password: 'Testing1234',
confirmPassword: 'Testing1234',
firstName: 'Abc',
lastName: 'Defg'
};
request(url)
.post('/user/signup')
.send(profile)
.end(function(err, res) {
if (err) {
throw err;
}
res.should.have.status(400);
done();
});
});
I also noticed that although I have declared var should = require('should'); , my ide notifies me that 'should' is a unused local variable. I don't really know why.

Try
res.status.should.be.equal(400);
or
res.should.have.property('status', 400);
And about " 'should' is a unused local variable". It's true. You don't use should directly. Only sideeffects. Try require('should'); instead.

Place the line:
require('should-http');
somewhere in your code. E.g.:
require('should-http');
describe('Test Something', function() {
...

As an addition to Yury answer. There is should-http package, which contain .status(code) assertion. You need to require somewhere it in code and it will be added to should.js.

I would switch to expect:
expect(res.status).to.be.eq(400);

Related

.save() not working inside callback function

I'm currently using the "request" module to get data from an external page:
if i use the following code, it doesn't work:
request(SITE_URL, function (error, response, body) {
var user = new gasStation({ id: 12345, name: 'Gustavo' });
user.save();
});
But if i make the call outside the request function it works as expected:
var user = new gasStation({ id: 12345, name: 'Gustavo' });
user.save();
request(SITE_URL, function (error, response, body) {
// some stuff
});
Why is this happening?
First never ignore your error handler's. Check if error is true. Also your URL might be malformed check that. Lastly make sure the mongoose model for user is initialized. I only saw initialization outside the request.

Ember data: Rollback createRecord on error

I'm trying to find the best way to avoid adding a record when there's an error using Ember Data:
This is my code:
createUser: function() {
// Create the new User model
var user = this.store.createRecord('user', {
firstName: this.get('firstName'),
lastName: this.get('lastName'),
email: this.get('email')
});
user.save().then(function() {
console.log("User saved.");
}, function(response) {
console.log("Error.");
});
},
I'm validating the schema on backend and returning a 422 Error in case it fails.
If I don't handle the error, the record is added to the site and I also get a console error.
So I did this:
user.save().then(function() {
console.log("User saved.");
}, function(response) {
user.destroyRecord();
});
Which kind of works deleting the record after reading the server response but:
1) I see the record appearing and dissapearing (like a visual glitch to say it somehow).
2) The console error still appears.
Is there a way to better handle this? I mean, is there a way to avoid adding the record when the server returns an error? Is there a way to avoid showing the console error?
Thanks in advance
You'll need to catch the error in the controller and then use deleteRecord() to remove it from the store:
actions: {
createContent() {
let record = this.store.createRecord('post', {
title: ''
});
record.save()
.then(rec => {
// do stuff on success
})
.catch(err => {
record.deleteRecord();
// do other stuff on error
});
}
}

Object #<Object> has no method 'User'

everyone. I have a problem with my code and Meteor 0.9.4
Here is my code:
Server/publications.js
Meteor.publish('getUsers', function () {
var loggedInUser = Meteor.User();
if (Roles.userIsInRole(loggedInUser, ['admin'])) {
return Meteor.users.find({}, {fields: {
_id: 1,
emails: 1,
roles: 1
}});
}
this.stop();
return;
});
Lib/router.js
Router.map(function() {
this.route('dashboardUsers', {
layoutTemplate: 'dashboardLayout',
path: "/dashboard/users",
waitOn: function() {
return Meteor.subscribe('getUsers');
}
});
});
When I run meteor app, I have the following error:
=> App running at: http://localhost:3000/
I20141019-18:21:50.827(4)? Exception from sub 8CRiG3Jmdv4mohPhd TypeError: Object #<Object> has no method 'User'
I20141019-18:21:50.949(4)? at null._handler (app/server/publications.js:3:31)
I20141019-18:21:50.950(4)? at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1594)
I20141019-18:21:50.950(4)? at _.extend._runHandler (packages/ddp/livedata_server.js:943)
I20141019-18:21:50.950(4)? at _.extend._startSubscription (packages/ddp/livedata_server.js:769)
I20141019-18:21:50.951(4)? at _.extend.protocol_handlers.sub (packages/ddp/livedata_server.js:582)
I20141019-18:21:50.951(4)? at packages/ddp/livedata_server.js:546
If you have a look at the docs for Meteor.user, you'll see it works everywhere except publish functions. You need to do:
var loggedInUser = Meteor.users.findOne(this.userId);
The error your getting is because of the upper-case 'u' in Meteor.User it should be Meteor.user.
However, as David Weldon pointed out you'll need to use Meteor.users.findOne(this.userId) inside of publish functions.
For anyone else running into the error Object #<Object> has no method 'user' when trying to access Meteor.user() be sure you have the accounts-base package.

Template.meteorError.rendered doesn't work in errors meteor package

I am trying to use meteor-errors package in my meteor project.
It has two main files with javascript code:
errors_list.js:
Template.meteorErrors.helpers({
errors: function() {
return Errors.collection.find();
}
});
Template.meteorError.rendered = function() {
var error = this.data;
Meteor.defer(function() {
Errors.collection.update(error._id, {$set: {seen: true}});
});
};
errors.js:
Errors = {
// Local (client-only) collection
collection: new Meteor.Collection(null),
throw: function(message) {
Errors.collection.insert({message: message, seen: false})
},
clearSeen: function() {
Errors.collection.remove({seen: true});
}
};
But looks like Template.meteorError.rendered method doesn't work. And I can't set status of elements from my error's collection as seen: true.
When I call for example:
(server)
throw new Meteor.Error 401, "You need to login to add a new address"
(client)
Meteor.call "verify_address", address, name, (error, result) ->
if error
Errors.throw error.reason
I got this error message in my markup, but my browser console shows:
Errors.collection.find().fetch()
[Object]
_id: "MZ8TzpsKCXaeC33Jn"
message: "Address not the correct size"
seen: false
__proto__: Object
And message's seen attribute is still "false".
The initial problem was that an error has a 'X' icon that does not remove the error when clicked. How are we supposed to remove the errors?
The reason for the strange behavior is, you are manually throwing a Meteor.Error whereas your template is bound to a collection which is populated by calling Errors.throw(message)
Try this:
Errors.throw('You need to login to add a new address');

webdriverjs: Expected string got an object

After discovering Mocha and webdriverjs, I wanted to give it a shot, after reading the readme.md in https://github.com/camme/webdriverjs, so i began with a trivial test.
var webdriverjs = require("webdriverjs"),
client = webdriverjs.remote(),
expect = require("chai").expect;
suite('Functional tests', function(done) {
setup(function(done) {
client.init().url('http://www.google.com', done);
});
test('test if you can open a firefox page', function() {
var inputType = client.getAttribute('#searchtext_home', 'type');
expect(inputType).to.be.a('string');
console.log(myString);
});
teardown(function(done) {
client.end(done);
//done();
});
});
Get the input element of google and expect its type is text.
I end up with an object in inputType variable.
AssertionError: expected { Object (sessionId, desiredCapabilities, ...) } to be a string
It does return an object from client.getAttribute(). So you should use it's 3rd parameter which is a callback function like this:
test('test if you can open a firefox page', function(done) {
client.getAttribute('#searchtext_home', 'type', function(err, inputType) {
expect(inputType).to.be.a('string');
done();
});
});
See more detail example code here.

Categories

Resources