Skip to content

Instantly share code, notes, and snippets.

@octoberstorm
Last active December 15, 2015 15:38
Show Gist options
  • Save octoberstorm/5283152 to your computer and use it in GitHub Desktop.
Save octoberstorm/5283152 to your computer and use it in GitHub Desktop.
Sử dụng TDD và Jasmine để phát triển Javascript trong Ruby on Rails

Tạo Rails project mới

rails new jstest
cd jstest

Thêm các gems cần thiết

# file Gemfile
gem 'jasmine'
gem "jasminerice"
gem "guard-jasmine"
gem 'guard-coffeescript'
gem 'rb-inotify', '~> 0.9'

Bundle

bundle

Khởi tạo các thư mục cần thiết cho Jasmine

rails g jasmine:install

Thiết lập cấu hình cho jasmine

# file {RAILS_ROOT}/spec/javascripts/support/jasmine.yml
src_dir: public/javascripts/compiled
src_files:
  - '**/*.js'
spec_dir: spec/javascripts/compiled
spec_files:
  - '**/*_spec.js'

Tải và cài đặt PhantomJS

Download Phantomjs

Khởi tạo Guard với Jasmine và Coffeescript

$ guard init

Tạo file spec/javascript/spec.js với nội dung sau

//= require application
//= require_tree .

Tạo file spec/javascripts/spec.css với nội dung

/*
 *= require application
 */

Khởi động Guard

guard

Viết test đầu tiên (Kết quả test là "Failed")

# {RAILS_ROOT}/spec/javascripts/calculator_spec.coffee

describe 'Calculator', ->

 it 'can add two positive numbers', ->
   calculator = new Calculator()
   result = calculator.add 2, 3
   expect(result).toBe 5

Viết code để đoạn test trên Pass

# {RAILS_ROOT}/app/assets/javascripts/calculator.coffee

window.Calculator = class Calculator
	add: (a, b) ->
		a + b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment