RSpec: Change the type of a spec regardless of the folder it lives in
In a Rails application, *_spec.rb
files get special treatment depending on the file's directory. E.g. when you put a spec in spec/controllers
your examples will have some magic context like controller
, post
or get
that appears out of nowhere.
If you want that magic context for a spec in another folder, use the :type
option:
describe CakesController, :type => :controller do
...
end
Related cards:
RSpec: How to test the content of a flash message in a request spec
The ActionDispatch module of Rails gives you the helper method flash
to access the flash messages in a response.
describe PostsController, type: :request do
descr...
JavaScript: Testing the type of a value
Checking if a JavaScript value is of a given type can be very confusing:
- There are two operators
typeof
andinstanceof
which work very differently. - JavaScript has some primitive types, like string literals, that are not objects (as oppose...
RSpec: Leverage the power of Capybara Finders and Matchers for view specs
View specs are a powerful tool to test several rendering paths by their cases instead of using a more costing feature spec. This is especially useful because they become quite co...
How to change the locale of a PostgreSQL cluster
There may be reasons to change the locale of your Postgres cluster. A popular one is your development system's locale being used by default (which may be annoying). Here is how to do that.
Beware: By following the steps below, you will drop and r...
How to find out the type of a model's attribute
When you want to find out the data type of an attribute, you can just use ActiveRecord's columns_hash
method.
It returns a hash of column objects that include a type
attribute (and more database-related information).
Example:
Contract.c...
RSpec 3 no longer chooses a spec's type based on its directory
While RSpec 1 and 2 decided that specs inside spec/model
are model specs, and those inside spec/features
are feature specs (and so on), RSpec 3 will no longer do that by default.
This will result in errors such as missing routing helpers, etc...
How to find out which type of Spec you are
When you need to find out in which kind of spec you are during run-time, it's definitely possible. It's a lot easier in RSpec 2+.
For example, consider this global before
block where you'd want to run some code for specific specs only:
c...
Cast a string to the type of an ActiveRecord attribute
ActiveRecord models know how to cast a given string to the type of a given attribute (or column).
The following model will serve as our example:
create_table :notes do |t|
t.string :title
t.integer :user_id
t.boolean :delet...
Test the content-type of a response in Cucumber
The step definitions below allow you to write this in both Webrat and Capybara:
When I follow "Download as PDF"
Then I should get a response with content-type "application/pdf"
Capybara
Then /^I should get a response with...
Change the color of a <hr> in all browsers
The following Sass will do it:
hr
color: #ddd
background-color: #ddd
border: none
height: 1px