Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: testing validation with resources #6

Open
bassettsj opened this issue Sep 12, 2014 · 1 comment
Open

Question: testing validation with resources #6

bassettsj opened this issue Sep 12, 2014 · 1 comment

Comments

@bassettsj
Copy link

So I wanted to make sure the validations were working in the defined resources. What do you think a good test would be for that?

  it('validates data before creation', function () {
    var invalidEvnt = angular.extend(testEvnt, {
      startDate: false // must be a Date object and must be less than endDate.
    });
    // Have to return something here;
    DS.expectCreate('evnt', invalidEvnt).respond(new Error('invalid'));

    Evnt.create(invalidEvnt).then(function (result) {
      fail('should have rejected');
    }, function (err) {
       //This will just return the error I created, thus the test is a little useless.
    });
  });
@jmdobry
Copy link
Member

jmdobry commented Sep 13, 2014

You're right, that test is useless.

If the test isn't useful without the functionality of the real data store, then the test needs to use the real data store.

The mock data store is useful when you want to test your own logic (the validate function), ignoring what the real data store would do (save to localStorage, make an http request, etc.). You didn't post your resource definition, but I'm guessing it's something like:

function validateEvnt(resourceName, attrs, cb) {
  if (!angular.isDate(attrs.startDate) || attrs.startDate < attrs.endDate) {
    cb('startDate must be a date and less than endDate!');
  } else {
    cb(null, attrs);
  }
}

var Evnt = DS.defineResource({
  name: 'evnt',
  validate: validateEvnt
});

So to test that your validate function does what it should:

it('should validate', function () {
  validateEvnt({
    startDate: false
  }, function (err, attrs) {
    assert.equal(err, 'startDate must be a date and less than endDate!');
  });
});

If you want to test that angular-data calls your validate function like it should, then you're welcome to contribute to angular-data's tests.

If you really want to test your validation function's integration with angular-data, then that test will need to use the real data store. All of the methods in angular-data-mocks are stubs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants