add helper generator

This commit is contained in:
John Bintz 2011-04-18 09:32:43 -04:00
parent bf001f8ba6
commit b7425b38a4
5 changed files with 46 additions and 11 deletions

5
.autotest Normal file
View File

@ -0,0 +1,5 @@
Autotest.add_hook(:initialize) do |at|
at.add_mapping(%r{bin/(.*)}, true) do |filename, matches|
at.files_matching(%r{spec/bin/#{matches[1]}_spec\.rb})
end
end

View File

@ -72,6 +72,11 @@ class BackboneGenerator < Thor
@name = name
%w{model view collection collection_view}.each { |which| send("generate_#{which}") }
end
desc 'spec-helper', "Generate a spec helper for Backbone things"
def spec_helper
template('spec_helper.js.erb', 'spec/javascripts/helpers/backbone_spec_helper.js')
end
end
BackboneGenerator.start

View File

@ -93,4 +93,12 @@ describe 'backbone-generator' do
should_generate_collection_view
end
end
describe 'spec helper' do
it "should generate a spec helper" do
system %{bin/backbone-generator spec-helper}
File.file?(collection = 'spec/javascripts/helpers/backbone_spec_helper.js').should be_true
end
end
end

View File

@ -1,21 +1,16 @@
describe('<%= object_name %>s', function() {
var collection, server;
var collection;
beforeEach(function() {
server = sinon.fakeServer().create();
});
afterEach(function() {
server.restore();
});
withServer();
it('should fetch records from the API', function() {
collection = new <%= object_name %>s();
server.respondWith('GET', '<%= underscore_name %>', [ 200, { 'Content-type': 'application/json' }, "[{id: 1}]" ]);
this.server.respondWith('GET', '<%= underscore_name %>', this.validJSONResponse([{id: 1}]));
collection.fetch()
server.respond();
this.server.respond();
expect(collection.length).toEqual(1);
});
}s);
});

View File

@ -0,0 +1,22 @@
/* Backbone helpers. Requires json2 and sinon-server or the full-blown sinon. */
var withServer = function() {
jasmine.getEnv().currentSpec.withServer();
};
if (isCommonJS) exports.withServer = withServer;
jasmine.Env.prototype.withServer = function() {
this.currentSuite.beforeEach(function() {
this.server = sinon.fakeServer().create();
});
this.currentSuite.afterEach(function() {
this.server.restore();
});
};
beforeEach(function() {
this.validJSONResponse = function(data) {
return [ 200, { 'Content-type': 'application/json' }, JSON.stringify(data) ];
};
});