Screencast: Tests mit Spork beschleunigen

Ab einer bestimmten Größe können die Testläufe die Entwicklung „verlangsamen“. Spork ist kann in diesem Fall Abhilfe schaffen, indem es die Ladezeiten verbessert. In Verbindung mit Guard bekommt man so schneller Rückmeldung über den aktuellen Stand. Ryan zeigt in diesem Screencast wie Spork eingesetzt werden kann.

 

Downloads in verschiedenen Formaten:

mp4
m4v
webm
ogg

 

Resourcen:

bash

[bash]
rspec .
time rspec .
bundle
spork –bootstrap
spork
rspec . –drb
guard init spork
[/bash]

Gemfile

[ruby]
group :test do
gem "spork", "> 0.9.0.rc"
gem "guard-spork"
end
[/ruby]

Guardfile

[ruby]
guard ’spork‘, :cucumber_env => { ‚RAILS_ENV‘ => ‚test‘ }, :rspec_env => { ‚RAILS_ENV‘ => ‚test‘ } do
watch(‚config/application.rb‘)
watch(‚config/environment.rb‘)
watch(%r{^config/environments/.+.rb$})
watch(%r{^config/initializers/.+.rb$})
watch(’spec/spec_helper.rb‘)
watch(%r{^spec/support/.+.rb$})
end

guard ‚rspec‘, :version => 2, :cli => "–drb", :all_on_start => false, :all_after_pass => false do
# …
end
[/ruby]

spec/spec_helper.rb

[ruby]
require ‚rubygems‘
require ’spork‘

Spork.prefork do
# …
RSpec.configure do |config|
# …
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
end
end

Spork.each_run do
# This code will be run each time you run your specs.
FactoryGirl.reload
end
[/ruby]

Screencast: Testen von Password-Reset und Erinnerung

In der letzten Woche zeigte Ryan wie man selber eine Funktion zum Zurücksetzen von Passwörter bauen kann. In dieser Woche zeigt er wie er diese Funktion mit RSpec, Capybara, Factory Girl und Guard testet.

 

Downloads in verschiedenen Formaten:

source code
mp4
m4v
webm
ogv

 

Resourcen:

bash

[bash]
bundle rails g rspec:install mkdir spec/support spec/models spec/routing guard init rspec gem install rb-fsevent rails g integration_test password_reset rails g controller password_resets new –no-test-framework rails g mailer user_mailer password_reset rails g migration add_password_reset_to_users password_reset_token:string password_reset_sent_at:datetime rake db:migrate
[/bash]

Gemfile

[ruby]
gem "rspec-rails", :group => [:test, :development] group :test do gem "factory_girl_rails" gem "capybara" gem "guard-rspec" end
[/ruby]

spec_helper.rb

[ruby]
require ‚capybara/rspec‘ RSpec.configure do |config| # … config.include(MailerMacros) config.before(:each) { reset_email } end
[/ruby]

spec/support/mailer_macros.rb

[ruby]
module MailerMacros def last_email ActionMailer::Base.deliveries.last end def reset_email ActionMailer::Base.deliveries = [] end end
[/ruby]

config/environments/test.rb

[ruby]
config.action_mailer.default_url_options = { :host => "www.example.com" }
[/ruby]

spec/requests/password_resets_spec.rb

[ruby]
describe "PasswordResets" do it "emails user when requesting password reset" do user = Factory(:user) visit login_path click_link "password" fill_in "Email", :with => user.email click_button "Reset Password" current_path.should eq(root_path) page.should have_content("Email sent") last_email.to.should include(user.email) end it "does not email invalid user when requesting password reset" do visit login_path click_link "password" fill_in "Email", :with => "nobody@example.com" click_button "Reset Password" current_path.should eq(root_path) page.should have_content("Email sent") last_email.should be_nil end it "updates the user password when confirmation matches" do user = Factory(:user, :password_reset_token => "something", :password_reset_sent_at => 1.hour.ago) visit edit_password_reset_path(user.password_reset_token) fill_in "Password", :with => "foobar" click_button "Update Password" page.should have_content("Password doesn’t match confirmation") fill_in "Password", :with => "foobar" fill_in "Password confirmation", :with => "foobar" click_button "Update Password" page.should have_content("Password has been reset") end it "reports when password token has expired" do user = Factory(:user, :password_reset_token => "something", :password_reset_sent_at => 5.hour.ago) visit edit_password_reset_path(user.password_reset_token) fill_in "Password", :with => "foobar" fill_in "Password confirmation", :with => "foobar" click_button "Update Password" page.should have_content("Password reset has expired") end it "raises record not found when password token is invalid" do lambda { visit edit_password_reset_path("invalid") }.should raise_exception(ActiveRecord::RecordNotFound) end end
[/ruby]

spec/models/user_spec.rb

[ruby]
describe User do describe "#send_password_reset" do let(:user) { Factory(:user) } it "generates a unique password_reset_token each time" do user.send_password_reset last_token = user.password_reset_token user.send_password_reset user.password_reset_token.should_not eq(last_token) end it "saves the time the password reset was sent" do user.send_password_reset user.reload.password_reset_sent_at.should be_present end it "delivers email to user" do user.send_password_reset last_email.to.should include(user.email) end end end
[/ruby]

spec/mailers/user_mailer_spec.rb

[ruby]
describe UserMailer do describe "password_reset" do let(:user) { Factory(:user, :password_reset_token => "anything") } let(:mail) { UserMailer.password_reset(user) } it "send user password reset url" do mail.subject.should eq("Password Reset") mail.to.should eq([user.email]) mail.from.should eq(["from@example.com"]) mail.body.encoded.should match(edit_password_reset_path(user.password_reset_token)) end end end
[/ruby]

Screencast: Dateiüberwachung mit Guard

Guard ist ein Tool für die Konsole, mit dem Veränderungen (FSEvent / Inotify / Polling) an Dateien überwacht werden können. Eine solche Funktion kann zum Beispiel für automatisierte Tests, Neustarts von Servern und viele andere Bereiche, in denen eine „Folgeaktion“ erforderlich ist, nützlich sein. In diesem Screencast gibt es eine kleine Einführung in Guard und wie es konfiguriert wird.

 

Download:

Download(13.8 MB, 8:44)
Alternativer Download für iPod & Apple TV(16 MB, 8:44)

 

Resourcen:

 

Quellcode:

[bash]
bundle
guard init rspec
guard init livereload
guard
[/bash]

[ruby]
# Gemfile
group :development, :test do
# …
gem ‚rb-fsevent‘, :require => false if RUBY_PLATFORM =~ /darwin/i
gem ‚guard-rspec‘
gem ‚guard-livereload‘
end

# Guardfile
guard ‚livereload‘ do
watch(%r{app/.+.(erb|haml)})
watch(%r{app/helpers/.+.rb})
watch(%r{public/.+.(css|js|html)})
watch(%r{config/locales/.+.yml})
end

guard ‚rspec‘, :version => 2 do
watch(%r{^spec/.+_spec.rb})
watch(%r{^lib/(.+).rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(’spec/spec_helper.rb‘) { "spec" }

# Rails example
watch(’spec/spec_helper.rb‘) { "spec" }
watch(‚config/routes.rb‘) { "spec/routing" }
watch(‚app/controllers/application_controller.rb‘) { "spec/controllers" }
watch(%r{^spec/.+_spec.rb})
watch(%r{^app/(.+).rb}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+).rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller).rb}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/requests/#{m[1]}_spec.rb"] }
watch(%r{^app/views/(.+)/}) { |m| "spec/requests/#{m[1]}_spec.rb" }
end
[/ruby]