I apply the QUnit to my Emberjs project to do unit/integration testing.
In unit testing we usually have to mock object/methods to test every condition.
I choose SinonJS as my mock library. It’s really easy to use.
Let’s see how to unit testing my components.
Testing html
Create a html that includes qunit,sinon,emberjs and your source codes. Remeber to use debug Emberjs(ember-1.x.x.js) not production(ember-1.x.x.min.js)
Make sure you invoke setupForTesting and injectTestHelpers after Application created.
In QUnit we could use module() to group test cases. test() after module() will be grouped.
Then we could add setup and teardown methods. They will be invoked in each test case.
lobby.spec.js
123456789101112131415161718
module("LobbyRoute",{setup:function(){MyApp.reset();},teardown:function(){//do something here.}});test("should render lobby template",function(){Ember.run(this,function(){route=MyApp.__container__.lookup('route:lobby');sinon.spy(route,"render");route.renderTemplate();ok(route.render.calledOnce,"render should be calledOnce");});});
Above example uses Ember’s __container__.lookup to find specific component and testing it.
But how we make sure the specified method is called?
Use sinon.spy to spy a object/method and check it.
defget_gc_version_from_userputs"Type a version d.d.d or leave blank to get the latest gc:"@gc_version=get_user_inputenddefget_gv_version_from_userreturnunless@config[:import_gv]puts"Type a version d.d.d or leave blank to get the latest gv:"@gv_version=get_user_inputenddefget_game_combo_version_from_userreturnunless@config[:import_game_combo]puts"Type a version d.d.d or leave blank to get the latest game combo:"@game_combo_version=get_user_inputend
After refactor, use array to create methods. define_method is used to define new methods dynamically.
do |argument| means your could pass parameters in. instance_variable_set is used to set a instance varaible
after_refactor
1234567
['gc','gv','game_combo'].eachdo|item|define_method("get_#{item}_version_from_user")do|argument|returnunlessargumentputs"Type a version d.d.d or leave blank to get the #{item}"instance_variable_set("@#{item}_version",get_user_input)endend
In my old emberjs project(money,food-ntpc)
i didn’t use ember-data to handle data processing with backend server.
The ember-data is closed to 1.0.0(now is 1.0.0.beta4) so i give it a try.
The official document is quite less and some posts in StackOverflow are out of date.
Checkout their source code is the quickest way to understand how they work and do customized.
I write down tips here for my reference in the future.
This article is the last one in 2013. In the rails project we use capybara to do UI testing.
Like click a button/link or fill the form. But i will show how to use capybara without rails in UI automation today.
Sometimes we have to repeat trivial works and these require user to interactive with browser.
For instance, upload file to server or input necessary data.
In orer to use capybara without rails. You need to require capybara manually.
In default capybara will launch rack server, change run_server to false.
Because we assume your server is running. Use selenium as driver.
12345678
require'capybara'require'capybara/dsl'Capybara.run_server=falseCapybara.current_driver=:selenium# it will open the browser to 127.0.0.1:8888/adminCapybara.app_host='http://127.0.0.1:8888/admin'
This week i write automatic utility tools for colleague.
It saves time and reduce trivial works. Learn useful ruby built-in methods.
List all under a folder
12345678910111213
# lists all files and folders under target/ folderDir.glob('target/**/**').selectdo|f|ifFile.directory?f# do somethingelse# do somethingendend# ONLY list all in target/ , doesn't list subfolders like target/subfolders/Dir.glob('target/**').selectdo|f|end
Today i push a new rails project to heroku called lunch.
I also push to github.
Every week day i and colleague have to discuss which lunch to eat.
It will be very helpful if a site can random pick a lunch restaurant for us. So i create a new rails project for this purpose.
The basic idea is simple, everybody can share a restaurant and add others’ restaurants to favorites list. Then site pick a restaurant for you.
I can study new stuff when i practice rails each time. I try to write down here.
In the past week i had tried mongodb in rails.
I rewrite my previous project money to use mongodb instead of active record.
The source code is in branch money-mongodb.
Here i record the steps of changing active record to mongodb
I read the javascript design pattern from Addy Osmani.
There’re code examples in the book and i try to re-write it by coffeescript.
It helps me understand pattern more and clear. During writing i found a good coffeescript cookbook and very helpful.
I put the final result to gist