add helper generator
This commit is contained in:
parent
bf001f8ba6
commit
b7425b38a4
|
@ -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
|
|
@ -72,6 +72,11 @@ class BackboneGenerator < Thor
|
||||||
@name = name
|
@name = name
|
||||||
%w{model view collection collection_view}.each { |which| send("generate_#{which}") }
|
%w{model view collection collection_view}.each { |which| send("generate_#{which}") }
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
BackboneGenerator.start
|
BackboneGenerator.start
|
||||||
|
|
|
@ -93,4 +93,12 @@ describe 'backbone-generator' do
|
||||||
should_generate_collection_view
|
should_generate_collection_view
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -1,21 +1,16 @@
|
||||||
describe('<%= object_name %>s', function() {
|
describe('<%= object_name %>s', function() {
|
||||||
var collection, server;
|
var collection;
|
||||||
|
|
||||||
beforeEach(function() {
|
withServer();
|
||||||
server = sinon.fakeServer().create();
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(function() {
|
|
||||||
server.restore();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should fetch records from the API', function() {
|
it('should fetch records from the API', function() {
|
||||||
collection = new <%= object_name %>s();
|
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()
|
collection.fetch()
|
||||||
server.respond();
|
this.server.respond();
|
||||||
|
|
||||||
expect(collection.length).toEqual(1);
|
expect(collection.length).toEqual(1);
|
||||||
});
|
});
|
||||||
}s);
|
});
|
||||||
|
|
||||||
|
|
|
@ -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) ];
|
||||||
|
};
|
||||||
|
});
|
Loading…
Reference in New Issue