teamon.eu

Webrat for [your language]

04 lutego 2010

Będzie szybko.

Webrat został napisany z myślą o Railsach. A co jeśli piszemy aplikacje w czymś innym? Odp. - Webrat! ;]

Wymagania: Ruby

Instalacja:

gem install mechanize webrat

Już.

Tworzymy sobie plik test.rb i wrzucamy:

 
require "webrat"
 
Webrat.configure do |config|
  config.mode = :mechanize
end
 
class MechanizeWorld < Webrat::MechanizeAdapter
  include Webrat::Matchers
  include Webrat::Methods
 
  Webrat::Methods.delegate_to_session :response_code, :response_body
end
 
Spec::Runner.configure do |config|
  include Webrat::Methods
end
 
# Tu sie zaczynaja testy
 
describe "Awesome system" do
  it "should work" do
    visit "http://0.0.0.0:8080"
    click_link "Login"
    fill_in "Username", :with => "teamon"
    click_button "Login"
    response_body.should include "Wrong password"
  end
end
 

Odpalamy przez

spec test.rb
(opcjonalnie:
spec --color --format=specdoc test.rb

 
% spec --color --format=specdoc test.rb
 
Awesome system
- should work
 
Finished in 0.060042 seconds
 
1 example, 0 failures
 

Bangla. Ale jest brzydko. Osobiście wole taka strukturę:

path/to/tests/spec_helper.rb
 
require "webrat"
 
Webrat.configure do |config|
  config.mode = :mechanize
end
 
class MechanizeWorld < Webrat::MechanizeAdapter
  include Webrat::Matchers
  include Webrat::Methods
 
  Webrat::Methods.delegate_to_session :response_code, :response_body
end
 
Spec::Runner.configure do |config|
  include Webrat::Methods
end
 
path/to/tests/spec.opts
 
--color
--format specdoc
 
path/to/tests/login_spec.rb
 
require File.join( File.dirname(__FILE__), "spec_helper" )
 
describe "Login system" do
  it "should not let me in" do
    visit "http://0.0.0.0:8080"
    click_link "Login"
    fill_in "Username", :with => "teamon"
    click_button "Login"
    response_body.should include "Wrong password"
  end
end
 
Rakefile
 
desc "Run specs"
task :spec do
  system("spec path/to/tests -O path/to/tests/spec.opts")
end
 

Odpalane oczywiście poprzez

rake spec

Pozostaje tylko pisać testy :)

Dodaj komentarz