Explore Blog posts about everything we are up to, Tech Talk videos covering a huge range of timely topics and Event listings to keep you current on happenings at the Labs.
Test pollution manifests itself as seemingly false negatives or false positives in a test suite. It occurs when some shared state is unintentionally modified, or unintentionally read and used in a test.
When test pollution builds up, it can mean that a project’s build fails unpredictably, which can stop a whole team from shipping code regularly. This is an expensive way to not build software. Continue reading →
This week started on the wrong foot with a 1 hour and 45 minute outage early Monday morning, that affected many of you. I’d like to apologize for this, and shed some light on what happened.
It began as a network outage at our hosting provider’s data center, which was resolved fairly quickly. Unfortunately, Tracker’s load balancers did not recover correctly, as they should have automatically, and to complete the perfect storm, the alerting mechanism that’s supposed to notify us and the hosting provider’s operations team did not work either due to misconfiguration.
The load balancer configuration issue has been corrected, and we’re confident that this problem will not occur again. Tracker’s production environment is designed to recover automatically from network and other types of outages, and our hosting provider monitors and responds to problems on a 24/7 basis.
To help you determine if Tracker is experiencing an outage in the future, we’ve enabled a public status page for Tracker, via Pingdom, one of the site monitoring services that we rely on.
When browsing a codebase, you often want to view the definition of a particular class, method, or variable. However, sometimes you want to do the reverse; you want to see where a particular class, method, or variable is used. Typically this involves using your editor’s text search, or a command line tool, such as grep or ack. Unfortunately, text searches may return false positives, such as log file data, or similarly named constructs. The problem is that you want to search code not text.
RubyMine has text search, but it also includes a powerful, code-based usage search. Usage search is smarter than text search, because RubyMine is aware of code constructs such as classes, methods, and variables. This results in a more accurate search; allowing you to quickly view and navigate actual usages. In this post, we’ll explore RubyMine’s usage search on OS X.
Find Usages
To find where a particular class, method, variable (instance or local), or parameter is used, place your cursor on it, and press alt/option + F7. Results will be displayed in the Find tool window.
command + alt/option + Up/Down navigates the results. Use F4 to jump to the source code of a particular usage.
Recent usage search results can be viewed again with command + E.
Find Usages in File
By default, find usages is scoped to the entire project. To scope it to the current file, use command + F7. command + G and command + shift + G navigate the results.
Show Usages
command + alt/option + F7 can be used to show usages inline. Instead of displaying usages in the Find tool window, they appear in a pop-up window.
Searching Code
RubyMine’s usage search demonstrates an advantage an IDE has over a text editor. By being aware of code constructs, such as classes, methods, and variables, IDEs can offer powerful tools that allow you to quickly and accurately explore a codebase.
Rails provides a handy 'Date.yesterday' method. This effectively uses 'Time.zone.today' to calculate yesterday as per the configured timezone.
Ruby's 'Date.today' method uses the system's timezone via a call to localtime(). Therefore, if your Rails app is configured in UTC and you live on the West Coast, tests that assert that 'yesterday < today' will start failing at around 5pm.
A multi-part series of articles on how to test Sencha Touch applications. It uses Jasmine for unit testing and Siesta for integration testing.
Part 1 – Getting Started
In this article you will learn how to set up an application to Jasmine tests in your
Opinionated is a good thing
In my not-so-humble professional opinion, every modern web framework should provide a testing infrastructure with each newly generated application. I’m not concerned if it isn’t my preferred testing package. As long as there’s something. Testing is not an option, and the framework authors probably (hopefully?) test, so why not offer a serving suggestion for new projects? The worst that can happen is that you, the developer, disagree with the choice of framework. There’s a little extra bootstrap cost to replace one framework with another. That’s far less expensive than every new developer discovering a way to test.
Sencha Touch 2.1 has a generator built into its sencha command line tool, but it does not create a test structure as part of the template. This article is the first in a series of discoveries about how to test Sencha Touch applications. I am not claiming that this is the one true way to test. This is not necessarily the best way, either. It is, however, something that works. It installs easily on my development laptop. It gets you to your first passing test quickly. It saves you the cost of exploring all of the options and making these discoveries for yourself. You have plenty of other things to worry about.
But first, you need a web server
Once you’ve installed Sencha Touch and Sencha Command (3.1.0.256 when I wrote this), and you’ve generated the template application, you’ll need to serve the pages locally. Most projects will have some sort of app server already running, but it’s not strictly necessary for testing. When I need to serve pages on my own, I prefer pow. It is a zero-configuration server that can host as many applications as you please. I also like the powder ruby gem; it adds a nice command line interface to manage pow. If you are worried about adding a ruby dependency to your project, stop worrying. Sencha Touch uses the compass ruby gem to generate css files; so you already have a ruby dependency.
pow looks for a rack app, but in my sample app, I don’t have one. pow also looks for a directory named public from which it will server static files. The simplest thing that works is to create a symlink named public that points to the root directory of the project.
# Generate a new ST app
$ cd <touch toolkit directory>
$ sencha generate app senchaBdd ~/workspace/sencha-bdd
# Set up pow/powder
$ cd ~/workspace/sencha-bdd
$ ln -s . public
$ powder up
$ powder link
# Test the server
$ open http://sencha-bdd.dev
If all goes well, you should be able to open the application in any web browser at http://sencha-bdd.dev
Running sample app
Install Jasmine
Installing the stand-alone version of Jasmine will work, but it doesn’t scale to hundreds or thousands of specs. That’s why the Jasmine gem was created. I did some more research and found a way to test using the Jasmine gem.
In the root directory of your project add rake and jasmine to your Gemfile
$ cat Gemfile
source "https://rubygems.org"
group :development do
gem 'rake'
gem 'jasmine'
end
Run bundle install
Run jasmine init
Jasmine will install a basic set up, but there’s some cruft that you won’t need for a Sencha application.
Ext.Loader.setConfig({
enabled: true, // Turn on Ext.Loader
disableCaching: false // Turn OFF cache BUSTING
});
Ext.Loader.setPath({
'SenchaBdd': 'app' // Set the path for all SenchaBdd.* classes
});
Ext.application({
name: 'SenchaBdd' // Create (but don't launch) an application
});
And this one in spec/javascripts/helpers/SpecHelper.js:
Ext.require('Ext.data.Model');
afterEach(function () {
Ext.data.Model.cache = {}; // Clear any cached models
});
var domEl;
beforeEach(function () { // Reset the div with a new one.
domEl = document.createElement('div');
domEl.setAttribute('id', 'jasmine_content');
var oldEl = document.getElementById('jasmine_content');
oldEl.parentNode.replaceChild(domEl, oldEl);
});
afterEach(function () { // Make the test runner look pretty
domEl.setAttribute('style', 'display:none;');
});
So, what’s going on here? Sencha Touch applications need Ext.Loader to manage class loading. You also need an Ext.Application, especially for controller tests. The modifications to jasmine.yml set up the proper load order, and the jasmine gem will find all of the source files underneath the app/ directory. The app.js is a customized version of your normal app.js that sets up the class loader and global namespace configuration. You should replace “SenchaBdd” with the real name of your application. Two things are happening in SpecHelper.js: First, by default Ext.data.Model caches every model created by the application in a global in-memory array. If you don’t clear it between tests, you can be surprised by test pollution. The second part is to set up and clear a space in the test runner for inserting DOM elements, usually for some sort of view testing.
Create a directory structure that matches your application’s
Your application’s directory structure should look something like this:
├── app
│ ├── controller
│ ├── model
│ ├── profile
│ ├── store
│ └── view
Modify the spec directory so that it mirrors the app/ directory:
You can get the stand alone version from http://github.com/pivotal/jasmine. Install it at the in the spec directory. I include the version number, so I can experiment with different versions, but that’s a matter of taste.
├── app
│ ├── controller
│ ├── model
│ ├── profile
│ ├── store
│ └── view
├── public -> .
├── resources/
├── spec/
│ ├── controller
│ ├── jasmine-1.3.1
│ ├── model
│ ├── profile
│ ├── store
│ └── view
└── touch
In order to get Jasmine going, you need a special html file named, by convention, SpecRunner. Add it to spec/ as well. It looks like this:
You will need to modify lines 18 and 22 with the name of your ST app (which can be found in app.json and app.js).
You should see the test results with one passing spec.
1 Passing Jasmine Spec
If you don’t see this, open up the browser’s developer console to look for clues.
Until next time
That’s it! You now have a complete JavaScript testing framework installed in your application. This is a good time to commit your changes. Celebrate in the glory of the green goodness. You’ve earned it.
Next time, I’ll show you how to test a model class.
REST principles by default is a fantastic convention within Rails applications. The documentation for how to route HTTP requests are comprehensive and give examples about photo resources within an application. If you’ve got photo and tag as first class resources of your application, Rails has you covered. But what if you are building an application with a focus on one type of resource, do you really want /resource_type as a prefix to all of your application paths? I certainly don’t and I’ll show you how to remove that without diverging from Rails core strenghts.
For better or worse, I’m always conscience of making sure applications I’m involved in have Cool URIs and sometimes that does mean fighting the Rails conventions. However Rails routing is very flexible and can provide me with the application paths that make me happy.
Take Twitter as an example. Every user has their username as a top level path, so instead of having /users/robb1e, they simply have /robb1e. When dealing with an application where there is one core resource it can make a lot of sense to strip the resource prefix. This can be achieved through scopes in the routing configuration.
Your::Application.routes.draw do
scope ":username" do
get '', to: 'users#show'
end
end
Gives you routes which look like
GET /:username(.:format) users#show
If you wanted to see the followers and followees of that user, you have two options. Return to the default resource or use HTTP verb contraints. I’ll show you both.
Your::Application.routes.draw do
scope ":username" do
get '', to: 'users#show'
resource :following, only: [:show]
resource :followers, only: [:show]
end
end
This adds the routes
following GET /:username/following(.:format) followings#show
followers GET /:username/followers(.:format) followers#show
Your::Application.routes.draw do
scope ":username" do
get '', to: 'users#show'
get '/following', to: 'user#following'
get '/followers', to: 'user#followers'
end
end
This gives the paths
GET /:username/following(.:format) user#following
GET /:username/followers(.:format) user#followers
If you are trending into paths unknown, you always have the safety of tests to help you out. Both Rails and RSpec have ways to test your application routes.
One gotcha which using the default resource routing removes is clashing paths. If you decide to build an admin page and want to put that at /admin, that needs to be in the routes config before the scoped block and if a user has given themselves the name of admin then you may be in for some fun.
So the next time a need arises for an unconventional route, check the documentation, it’s probably possible although almost always warants thinking about.