Skip to content

Instantly share code, notes, and snippets.

@p-baleine
Created August 5, 2012 07:58
Show Gist options
  • Save p-baleine/3262747 to your computer and use it in GitHub Desktop.
Save p-baleine/3262747 to your computer and use it in GitHub Desktop.
stubbing amd module
require.config({
urlArgs: (new Date()).getTime(),
baseUrl: './'
});
window.createContext = stubContext({
shim: {
'underscore': {
exports: '_'
},
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
}
},
urlArgs: (new Date()).getTime(),
baseUrl: './',
paths: {
jquery: 'libs/jquery.min',
underscore: 'libs/underscore-min',
backbone: 'libs/backbone-min'
}
});
require([
'spec/models/todo.spec'
, 'spec/collections/todos.spec'
, 'spec/views/todolist.spec'
]);
// https://coderwall.com/p/teiyew
function stubContext(config) {
return function(stubs) {
var map = {},
context;
_.each(stubs, function(val, key) {
var stubName = 'stub_' + key;
map[key] = stubName;
});
context = require.config(
_.extend({
context: Math.floor(Math.random() * 1000000),
map: {
"*": map
}
}, config)
);
_.each(stubs, function(val, key) {
var stubName = 'stub_' + key;
define(stubName, function() {
return val;
});
});
return context;
};
}
$(function() {
var todoItem = new Backbone.View(),
TodoItemStub = sinon.stub().returns(todoItem),
stubs = {
'views/todo': TodoItemStub
},
context = createContext(stubs);
context(['views/todolist'], function(TodoList) {
describe('TodoList', function() {
beforeEach(function() {
this.todoList = new TodoList();
});
describe('描画', function() {
beforeEach(function() {
this.todo1 = new Backbone.Model({ id: 1 });
this.todo2 = new Backbone.Model({ id: 2 });
this.todo3 = new Backbone.Model({ id: 3 });
this.todoList.collection = new Backbone.Collection([
this.todo1,
this.todo2,
this.todo3
]);
this.todoList.render();
});
it ('各todoモデルに対してTodoItemビューを描画すること', function() {
expect(TodoItemStub).toHaveBeenCalledThrice();
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment