View

Cucumber: How to speed up your test suite

In most projects I know, Cucumber test suite speed is not an issue. Of course, running 350 features takes its time, but still each test for itself is reasonably fast. There is nothing you can do to fundamentally speed up such a test (of course, you should be using parallel_tests).

However, in projects that go beyond clicking around in a web GUI and checking results, there might be various measures to speed up things. Katapult tests, for example, could be sped up more than 4 times by …

Repeats

RSpec: Tagging examples and example groups

In RSpec you can tag examples or example groups with any tags you like simply by saying

describe ReportCreator, slow: true do
  # ..
end

describe ReportCreator do
  it 'generates reports', slow: true do
    # ...
  end
end

You can then only run examples with these tags.

rspec --tag slow
rspec -t slow

# Using the parallel_tests gem
rake "parallel:spec[,,--tag slow]"

Or you can run all examples except the ones with a certain tag:

```
rspec –tag ~slow # note the ~
rspec -t ~slow

Using the parallel_tests gem

rake "par…

Repeats

Advisory: Excel converts CSV entries to formulas

If your application exports CSV, be advised that Excel and other spreadsheet applications treat certain cells (those starting with =, +, - or @ as formulas.

This is an issue if you output user input. Not only is it probably not what you want, it also poses a security risk. See the link for attack vectors.

Note that current Excel versions will warn the user when opening the file. At least for the code execution vulnerability, this warning seems adequate to me.

Solution

If possible, always **try to avoid CSV and output XLS(X) f…

Auto-destruct in 51 days

Updated: When overriding #method_missing, remember to override #respond_to_missing? as well

You should override #respond_to_missing? instead of #respond_to? so your instances work with #method. Also see Marc's post.

Using Rails with Webpack: First steps

Webpack opens the world of npm to Rails developers. It does not collide with the asset pipeline—however, it's kind of a parallel world and you'll probably be better off going for either Webpack or asset pipeline.

Prerequisites

  1. Install a recent Node.js

    curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
  2. Install Yarn

    curl -sS https://dl.yarnpkg.com/debian/pubk...
    
Repeats

BigDecimal arithmetic in Ruby

Ruby comes with a class BigDecimal which you can use for arbitrary precision arithmetic. You should use BigDecimal instead of Float whenever you care about rounding errors, e.g. whenever you are dealing with money.

You should remember these two rules when working with BigDecimal values:

  • When you add or multiply a BigDecimal with another BigDecimal, the …

Turning off VCR when stubbing with Webmock

Sometimes when working with VCR you still want to use webmock. Since VCR hooks into webmock and fails when an unknown request is happening, you have to turn it off in order to use webmock like you are used to. Here is how to do this in rspec.

RSpec.configure do |config|
  config.around do | example |
    if example.metadata[:turn_off_vcr]
      VCR.turn_off!
      example.run
      VCR.turn_on!
    else
      example.run
    end
  end
end
External contentRepeats

Sidekiq: Problems and Troubleshooting

When using Sidekiq in your application, you must write thread-safe code.

This wiki page also lists gems that are known to be unsafe on threaded applications.
When adding a gem that will also be used by a Sidekiq worker, make sure to confirm it's thread-safe.

Webservice to test and inspect requests

Requestb.in is a webservice that gives you a temporary URL you can use to test request. The page will automatically record and display the latest web request made to it.

How to install packages from newer Ubuntu releases

We're usually running Ubuntu LTS versions. Sometimes newer hardware requires packages from more recent Ubuntu releases that only come with 6 months of support. If there is really no other way, it's possible to install packages from later Ubuntu releases

Caution: Pay really close attention to what you're doing. Depending on the package, this process may require upgrading a lot of dependencies, possibly breaking the system! You really should not do this unless you've carefully calculated the impact on your system

Preparation

First,…

Auto-destruct in 46 days

Updated: Chrome 34+, Firefox 38+, IE11+ ignore autocomplete=off

Added new method with autocomplete="new-password".

Selenium: How to close another tab (popup)

If you open a pop-up window [1] in your Selenium tests and you want to close it, you can do this:

# Find our target window
handle = page.driver.find_window("My window title")

# Close it
page.driver.browser.switch_to.window(handle)
page.driver.browser.close

# Have the Selenium driver point to another window
last_handle = page.driver.browser.window_handles.last
page.driver.browser.switch_to.window(last_handle)

Mind these:

  • find_window returns a window handle, which is something like `"{485fa8bd-fa99-…

Rails + Sidekiq::Web: Configuration for wildcard session cookies

When you're using Sidekiq::Web to monitor the Sidekiq status AND have your session cookie configured to a wildcard domain like .example.com, you need to take an additional step to keep your cookies valid.

Issue

Sidekiq::Web is mounted into your Rails application and will use the Rails session cookie for protection from CSRF attacs. While it somehow figures out the cookie name, it does NOT respect cookie configuration like a custo…

Repeats

Reading an element's attributes with Capybara

capybara_element['attribute_name'] allows accessing an element's attributes in Capybara.

A few examples:

find('#my_element')['class']
# => "first-class second-class"

find('#my_input')['placeholder']
# => "My placeholder value"

find('a#example-link')['href']
# => "http://example.com"

find('#my_element')['missing_attribute']
# => nil

Fix for: "can't convert nil to String" when running "gem update --system"

When attempting to update RubyGems, depending on updates your previously performed, you might run into an error

ERROR:  While executing gem ... (TypeError)
can't convert nil into String

Fix this by running

gem uninstall rubygems-update -a -x
Repeats

Don't require files in random order

A common pattern in Ruby is to to require all files in a specific diretory, using something like

Dir.glob(Rails.root.join('lib/ext/**/*.rb')).each do |filename|
  require filename
end

However, this causes files to be required in an order determined by the file system. Since load order can be important, this may lead to different behavior on different machines which are hard to debug.

Simply add a .sort:

Dir.glob(Rails.root.join('lib/ext/**/*.rb')).sort.each do |filename|
  require filename
end
Auto-destruct in 51 days

Updated: Git: How to configure git to push only your current branch

Added info about a small footgun with the current setting most of us are using. It's still the best option all around.


For most of us, current is the safest push behavior. However, it has a small side effect: When a branch with your local name does not exist on your remote, it automatically creates it on the remote.

While that is usually what you want, it does not setup tracking between your local and remote branches. That means git pull won't work, and Git won't notify you when your local and remote …

Repeats

MySQL will not use indexes if you query the wrong data type

When MySQL refuses to use your index, you can do a number of things wrong. One of them might be conditions with improper data types.

An example

For example, let's assume you have a users table with an email field (varchar) which is indexed.

MySQL will use the index when your query is well-formed:

mysql> EXPLAIN SELECT * FROM users WHERE email = 'foo@example.com';
+----+-------------+-------+-------+----------------------+----------------------+---------+-------+------+-------+
| id | select_type | table | type  | poss...
View
3390 cards