When you roll custom URLs with hacks like routing-filter, you can put a spec like this into spec/routing/routing_spec.rb
:
Spreewald has steps that let you test that e-mails have been sent, using arbitrary conditions in any combination.
The attached file is for legacy purposes only.
Don't use should validate_format_of(...)
because that matcher works in weird ways. Use the allow_value
matcher instead:
describe Email, '#sender' do
# > Rspec 3 should syntax
it { should allow_value("email@addresse.foo").for(:sender) }
it { should_not allow_value("foo").for(:sender) }
# Rspec 3 expect syntax
it { is_expected.to allow_value("email@addresse.foo").for(:sender) }
it { is_expected.not_to allow_value("foo").for(:sender) }
end
Errors that may occur if you do use should validate_format_of(...)
:
...
You can write regular expressions some different ways, e.g. /regex/
and %r{regex}
. For examples, look here.
Remember that it is always a good idea to match a regex visually first.
Literal Characters
[ ] \ ^ $ . | ? * + ( )
Character Classes
[ae] matches a and e, e.g. gr[ae]y => grey or gray => but NOT graay or graey
[0-9] ...
The following example is from the Cucumber wiki:
Given a blog post named "Random" with Markdown body
"""
Some Title, Eh?
==============
Here is the first paragraph of my blog post. Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
"""
That multi-line string will be given as the last block argument in your step definitions:
Given /^a blog post named "([^\"]*)" with Markdown body$/ do |title, markdown|
Post.create!(:title =...
Our awesome collection of rspec helpers (formerly known as "spec_candy.rb") is now available as a gem. It works, it is tested and there will be updates.
Add rspec_candy
to your Gemfile.
Add require 'rspec_candy/helpers'
to your spec_helper.rb, after the rspec requires.
An alternative to this technique is using VCR. VCR allows you to record and replay real HTTP responses, saving you the effort to stub out request/response cycles in close details. If your tests do require close inspection of requests and responses, Webmock is still the way.
WebMock is an alternative to FakeWeb when testing code that uses the network. You sh...
This will tunnel HTTP requests to one given domain and port through an intermediary SSH server:
ssh -L 8080:targethost:80 tunnelhost
http://localhost:8080 will now connect you to http://targethost:80, tunnelling all data through tunnelhost
via SSH.
Note that the connection between tunnelhost and targethost will still be unencrypted in this example.
For example, to send a form and populate a preview div with the response.
$('content_form').request({
parameters: { 'preview': "1" }, // overrides parameters
onComplete: function(transport){
$('previewContent').update(transport.responseText);
}
});
When a Cucumber feature leaves your page through an external Link, Webrat has problems like "Could not find field: "E-mail" (Webrat::NotFoundError)
" using your page afterwards. It will also have trouble following redirects.
Fix it with this step:
Given /^I am back on my page$/ do
webrat_session.header("Host", "www.example.com")
end
This may be awkward to set up, but will work once you're done.
Fun facts:
STOP SLAVE; RESET SLAVE;
and reset your my.cnf) and restart the MySQL daemon.Create replication user
: In the MySQL shell:
CREATE USER 'replicator'@'%' IDENTI...
Most of these will not work in newer projects because these use the Capybara/Rack::Test combo in lieu of Webrat.
Find input fields
Then /^there should be a "([^"]+)" field$/ do |name|
lambda { webrat.current_scope.send(:locate_field, name) }.should_not raise_error(Webrat::NotFoundError)
end
Then /^there should be no "([^"]+)" field$/ do |name|
lambda { webrat.current_scope.send(:locate_field, name) }.should raise_error(Webrat::NotFoundError)
end
Find html content
Then /^I should see "([^\"]*)...
With defaults, RCov doesn't work the way you how you would like it to. To create a nice test coverage report, copy the attached file to lib/tasks/rcov.rake
. After that rake rcov:all
will run all RSpec examples and Cucumber features. The report will be written RAILS_ROOT/coverage/index.html
.
Here is what the task does in detail:
app/**/*.rb
and nothing elseIGNORE_SHARED_TRAITS=true
it ...To test concurrent code, you will need to run multiple threads. Unfortunately, when you use blocking system calls (e.g. locks on the database), Ruby 1.8 threads won't work because system calls will block the whole interpreter.
Luckily you can use processes instead. fork
spins off a new process, IO.pipe
sends messages between processes, Process.exit!
kills the current process. You will need to take care of ActiveRecord database connections.
Here is a full-fledged example:
describe Lock, '.acquire' do
before :each do
...
rspec_spinner is a progress bar for RSpec which outputs failing examples as they happen (instead of all at the end).
gem install rspec_spinner
script/spec -r rspec_spinner -f RspecSpinner::Bar -c
alias ss='script/spec -r rspec_spinner -f RspecSpinner::Bar -c'
There's also an alternate runner RSpecSpinner::Spinner
which shows a spinner and the name of the current spec instead of a progress bar.
sudo gem install parallel
script/plugin install git://github.com/grosser/parallel_tests.git
config/database.yml
test:
database: xxx_test<%= ENV['TEST_ENV_NUMBER'] %>
script/dbconsole -p
CREATE DATABASE `xxx_test2`;
...
script/generate rspec
(you'll probably only let it overwrite files in script/
)
Prepare test databases...
sudo gem install gettext --no-ri --no-rdoc
sudo gem install fast_gettext --no-ri --no-rdoc
script/plugin install git://github.com/grosser/gettext_i18n_rails.git
(didn't work as gem)cp locale/app.pot locale/de/app.po
for every locale you want to use_('text')
in your rails coderake gettext:find
to let GetText find all translations usedWhen you need to patch an existing gem, one way is to "vendor" the gem by copying it into the vendor/gems
directory of your Rails project. You can then make any changes you require and Rails will use the vendored version of the gem after a server restart. Unfortunately you need to perform some additional steps to marry Rails and the copied gem. This notes describes what to do.
This is super-painful. If you just copy the gem to vendor/gems
, Rails will complain:
Unpacked gem foolib in vendor/gems has no s...
This note shows how to merge an ugly feature branch with multiple dirty WIP commits back into the master as one pretty commit.
git rebase
What we are describing here will destroy commit history and can go wrong. For this reason, do the squashing on a separate branch:
git checkout -b squashed_feature
This way, if you screw up, you can go back to your original branch, make another branch for squashing and try again.
Tip
If you didn't make a backup branch and something ...
To run a single test file:
rake test:units TEST=test/unit/post_test.rb
rake test:functionals TEST=test/functional/posts_controller_test.rb
rake test:integration TEST=test/integration/admin_news_posts_test.rb
You may even run a single test method:
ruby -I test test/unit/post_test.rb -n "name of the test"
ruby -I test test/functional/posts_controller_test.rb -n test_name_of_the_test # underscored, prefixed with 'test_'
Or all tests matching a regular expression:
ruby -I test test/integration/admin_news_posts_test.r...
Please keep this config simple. It should be a starting point for new developers learning Git.
[user]
name = Your Name
email = your.name@domain.com
[branch]
sort = -committerdate
[color]
ui = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
whitespace = white reverse
meta = blue reverse
frag = blue reverse
old = red
new = green
[color "status"]
added = green
changed = yellow
untracked = cyan
[interactive]
singlekey = true # Do not requir...
This is for people recovering from Subversion.
git clone git@example.com:repositoryname
git status
git commit -m "good description"
git push
git pull
git add file
git add .
git checkout -b branchname
...
This project is (or will be) a best effort semi-static verifier for your Ruby on Rails projects. Delivered as a Ruby gem it provides a shell command task "railscheck" that you can run against your Rails projects to test for a number of typical bugs, potential problems and inconsistencies.