softshipper
softshipper

Reputation: 34081

Pass over throwned error with completeError

I try to write an application that insert record into mongodb.
First look at my test:

test('Password test failed, not strong enough.', () {

    Account.create({'name': 'eric', 'email': '[email protected]', 'password': 'Test'})
        .catchError((err) {
            expect(err, throwsA(new isInstanceOf<DuplicateError>()));
        });
});

This test should be failed, because the password is not strong enough. And the code, that try to insert record.

static Future<String> create(Map account) {

var completer = new Completer();
String hashed_password;
var self = new Account();

if(self._signUpKeys.length != account.length) {
    return completer.completeError(new LengthError(I18n.instance.getTextByMap('TEXT1')));
}

for(var singUpKey in self._signUpKeys) {

    if (!account.containsKey(singUpKey)) {

        return completer.completeError(new ArgumentError(I18n.instance.getTextByMap('TEXT1')));
    }
}

// Try to find duplication
Future.wait([findAccountByField('name', account['name']),
             findAccountByField('email', account['email'])])
    .then((Iterable<Map<String, String>> lists) { 

        // Check of any duplications
        lists.forEach((value){
            value.forEach((String key, String value) {
                switch(key) {
                    case('name'):
                        return completer.completeError(new DuplicateError(
                                I18n.instance.getTextWithMarker('TEXT2', {'&1': value})));
                    case('email'):
                        return completer.completeError(new DuplicateError(
                                I18n.instance.getTextWithMarker('TEXT3', {'&1': value})));
                }

            });

            hashed_password =  Account.generateHashPassword(account['password']);
            self._insert(self._fillDbFields(name: account['name'], email: account['email'], hashed_password: hashed_password,
                                            created_at: new DateTime.now(), activated: false))
                .then((result) => completer.complete(result));
        });
    })
    .catchError((err) {
        completer.completeError(err);
    });

return completer.future;

}

this allocation will thrown an error, because the password is not according to security requirement.

hashed_password =  Account.generateHashPassword(account['password']);

and this part should catch error and complete it.

    .catchError((err) {
        completer.completeError(err);
    });

but in the test, I've got NoSuchMethodError. Why here, the error object is not pass over to test? What i do here wrong?enter image description here

Upvotes: 2

Views: 92

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657406

I think you should check what value lists has here

Future.wait([findAccountByField('name', account['name']),
             findAccountByField('email', account['email'])])
    .then((Iterable<Map<String, String>> lists) { 

if it is null you can't call forEach on it

Upvotes: 2

Related Questions