jmeas/backbone.radio — pull request #46 by thejameskyle (2014-06-01 00:23:07+00:00)
Title: Refactor Tests

I started on some stuff and kinda went crazy.. I'll try my best to detail everything I did.

**Changes:**
- Move `spec/radio.js` tests into `spec/channel.js` (ref: #45)
- Reduce repetition in tests by taking advantage of beforeEach/afterEach and nested describes (ref: #30)
- Use deep equal where appropriate
- Don't use unnecessary spies
- Use better assertions
- Use `_.clone(Object)` instead of `_.extend({}, Object)`

The DEBUG mode tests were all different, so I flattened the `describe`/`beforeEach` blocks into just `it` blocks so it would read easier

```js
this.reactStub = this.sinon.stub(this.channel, 'react');
this.reactOnceStub = this.sinon.stub(this.channel, 'reactOnce');
this.stopReactingStub = this.sinon.stub(this.channel, 'stopReacting');
this.commandStub = this.sinon.stub(this.channel, 'command');
// instead:
this.channelStub = this.sinon.stub(this.channel);
// as long as you don't need to actually call anything on this.channel
```

```js
this.callback = function() { return 'the thing'; };
this.callbackSpy = this.sinon.spy(this.callback);
// instead:
this.callbackStub = this.sinon.stub().returns('the thing');
```

```js
// use these properly
this.sinon.spy(this.object, 'function_that_i_want_to_call_through');
this.sinon.stub(this.object, 'function_that_i_dont_want_to_call_through');
```

```js
expect(this.channel._channelName).to.equal(this.channelName);
// instead:
expect(this.channel).to.have.property('_channelName', this.channelName);
```

```js
this.channelName = this.channel._channelName;
this.channelKeys = _.keys(this.channel);
this.eventKeys = _.keys(Backbone.Events);
this.requestKeys = _.keys(Backbone.Radio.Requests);
this.commandKeys = _.keys(Backbone.Radio.Commands);
this.eventIntersect = _.intersection(this.eventKeys, this.channelKeys);
this.requestIntersect = _.intersection(this.requestKeys, this.channelKeys);
this.commandIntersect = _.intersection(this.commandKeys, this.channelKeys);
expect(this.eventIntersect.length).to.equal(this.eventKeys.length);
expect(this.requestIntersect.length).to.equal(this.requestKeys.length);
expect(this.commandIntersect.length).to.equal(this.commandKeys.length);
// instead:
expect(this.channel).to.contain(Backbone.Events);
expect(this.channel).to.contain(Backbone.Radio.Requests);
expect(this.channel).to.contain(Backbone.Radio.Commands);
```

```js
expect(this.channelStub.listenTo).to.have.been.calledOnce;
expect(this.channelStub.listenTo).to.have.been.calledWithExactly('foo1', 'bar1');
// instead:
expect(this.channelStub.listenTo).to.have.been.calledOnce.and.calledWithExactly('foo1', 'bar1');
```

```js
expect(function() { ... }).not.to.throw(Error);
// instead:
this.stopReacting = function() { ... };
expect(this.stopReacting).not.to.throw(Error);
```

You might just want to open the files side by side and go through them that way. The diff is gonna suck to go through