Friday, February 27, 2015

Stubbing Global Methods in JS Specs with Sinon

In your Rails app, your JS should be tested too! Setting that up is a topic for another post. When testing your JS, a lot of the same philosophies apply. One of those is mocking methods other than the one you're testing. How do you do this for methods in other libraries, such as vanilla JS or jQuery?

Vanilla JS (the confirm method):
 sinon.stub(window, 'confirm')  
jQuery (the fadeOut method):
 sinon.stub(jQuery.prototype, 'fadeOut')  

Monday, February 23, 2015

Cleaning out server assets in development

Depending on your server settings (config/environments/development.rb), some of your assets (css, js, images, etc.) may end up being cached by the server and won't reflect your changes. Fortunately, there is a command to clear all of these out so they will be regenerated. With the server shut down, run rake assets:clobber and all of your asset caches will be removed. The next time you run the server and navigate to your app, the assets will be regenerated with the current version of the files.

Thursday, February 19, 2015

Using self.included to Include Dependencies

As part of a project I'm working on, I created a mountable engine for extracting all authentication concerns for the app. I created a TestHelper module that includes methods for mocking various aspects required for authentication. These methods require Devise::TestHelpers to function properly, and my goal was for the calling app to not know or care about this dependency. To solve this, I used self.included to automatically include the helper when necessary (in my case, only in controller specs).
Here is the code:

 def self.included(receiver)  
  if defined?(receiver.metadata) && receiver.metadata[:type] == :controller  
   receiver.send :include, Devise::TestHelpers  

Sunday, February 15, 2015

Testing Multiple Attributes of an Input Field

Sometimes in a view or feature spec, you want to test multiple attributes of an input field (or some other field). For example, you might want to test that the input field is a checkbox and is checked, as well as testing that the field has the id #rawr. This is how you would do that in a view spec (feature spec is similar):

 expect(content).to have_css("input[type='checkbox'][checked='checked']#rawr")  

Monday, February 2, 2015

Updating a Rails Model with a Non-default Primary Key

In my last post, I talked about creating a Rails model with a non-default primary key. You can create new instances of the model with no problem, but what happens when you want to update an instance of the model?
 TypeError: nil is not a symbol nor a string  
Oh no!!!!

Fortunately, there is an easy fix. You just have to specify the column name of the primary key from within the model.
 class BearHabitat < ActiveRecord::Base  
   self.primary_key = 'bear_id'  
Now you should be able to update your model within your code with no problem. Why does this happen in the first place? My guess is that somewhere within ActiveModel, it assumes that the primary_key field is set. Since your migration set id to false, this value gets defaulted to nil. If you don't manually set it, ActiveModel gets sad. Now you can make ActiveModel happy again.