I'm working on a jasmine extension that I'm calling jasmine-ng-bdd. As the
name suggests, it provides support for BDD-style tests with explicit support
for Angular applications.

Before getting too much further done the path of developing this thing, I'd
like some input from the community on what they'd like to see for the syntax
of the given/when/then statements.

In the spirit of the topic of this discussion, let me set it up with a 
"given", "when",
and "then".

GIVEN: An Angular application like the following. (NOTE: the reason for the
myControllerFn will become clear in the "when" section below.)

var app, myControllerFn;

    app = angular.module('MyApp',[]);
    myControllerFn = function($scope) {
        $scope.price;
        $scope.discount;
        $scope.applyDiscount = function () {
            $scope.price = $scope.price - ($scope.price * $scope.discount);
        };
    };
    app.controller('MyController', ['$scope', myControllerFn]);

WHEN: You write tests for it like this.

    describe('MyApp', function () {

    var dep;

    beforeEach(function () {
     // spyOnController is a function provided by jasmine-ng-bdd
     // that returns an object containing a property for each 
     // dependency injected into the controller. Further, the functions
     // on these dependencies will be spied on. The spyOnController
                // function uses the injector's annotate function on 
myControllerFn
                // to get the list of injected dependencies.
        dep = spyOnController(myControllerFn);
    });

    it('should properly calculate the discounted price', function () {
     // ** See options below. **
    });

});


THEN: How would folks like to write the given/when/then statements?

Here are the options currently under consideration:

1. Follow the strategy used by jasmine for creating spies. This is probably
the easiest to implement.

given(dep.$scope, "price").is(100);
  and(dep.$scope, "discount").is(.10);
when(dep.$scope, "applyDiscount").called();
then(dep.$scope, "price").is(90);

2. Only pass the object context to the given/when/then functions, and use 
that
to dynamically build out a custom API for that object.

given(dep.$scope).price.is(100);
  and(dep.$scope).discount.is(.10);
when(dep.$scope).applyDiscount.called();
then(dep.$scope).price.is(90);

3. Same as above, only a little more code-like for the givens and whens. 

given(dep.$scope).price = 100;
  and(dep.$scope).discount = .10;
when(dep.$scope).applyDiscount();
then(dep.$scope).price.is(90);

NOTE: You'd still have less code-like statements for training spies in the
given statements, e.g. given(dep.$scope).calculateTax.throws("Tax rate not
found for state: 'TX'.").

Are there other options folks would like to suggest?

Thanks,

-michael

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to