Skip to content

Instantly share code, notes, and snippets.

Created January 25, 2013 15:50
Show Gist options
  • Save anonymous/4635437 to your computer and use it in GitHub Desktop.
Save anonymous/4635437 to your computer and use it in GitHub Desktop.
angular and coffeescript in rails
// app/assets/javascripts/ng_app/controllers.js
function PostListCtrl($scope) {
$scope.posts = [
{ "title": "My First Post", "intro": "Subtitle of my post" },
{ "title": "Another Post", "intro": "Something interesting about this post" },
{ "title": "One More Thng", "intro": "There's always something more" }
]
}
// this runs correctly in the browser
# app/assets/javascripts/ng_app/controllers.js.coffee (replaces the js version)
app = angular.module 'myapp', []
$ ->
angular.bootstrap $("#myapp"), ["myapp"]
app.controller "PostListCtrl", ($scope) ->
$scope.posts = [
{ "title": "My First Post", "intro": "Subtitle of my post" },
{ "title": "Another Post", "intro": "Something interesting about this post" },
{ "title": "One More Thng", "intro": "There's always something more" }
]
# note, I had to manually bootstrap it to get it to work
# on the page, replaced <div ng-app> with <div id="myapp">
# it runs correctly in the browser
# but the test reports
# ReferenceError: PostListCtrl is not defined in http://localhost:3000/assets/controllers_spec.js?body=1 (line 7)
# how do i get the tests to find it?
# spec/javascripts/controllers_spec.coffee
describe 'myapp controllers', ->
describe 'PostListCtrl', ->
it 'creates "posts" model with 3 posts', ->
scope = {}
ctrl = new PostListCtrl(scope)
expect(scope.posts.length).toBe(3)
# this passes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment