{ build(:scores, data: data) } it 'builds a parsed XML document from the data' do subject.document.at('.//event/description').text.should == 'Boston vs. Montreal' end def build_xml(opts) builder = Builder::XmlMarkup.new builder.event do |event| event.description "#{opts[:home_team]} vs. # {opts[:away_team]}" # ... end end
{ create(:user) } it "returns the user's credit card" do mock_http = stub mock_http.stubs(get: { number: 1234 }) Net::HTTP.stubs(new: mock_http) subject.card_number.should == '1234' end end
{ create(:user) } it "returns the user's credit card" do mock_http = stub mock_http.stubs(get: { number: 1234 }) Net::HTTP.stubs(new: mock_http) subject.card_number.should == '1234' end end
create(:user) } it "returns the user's credit card" do card = CreditCard.new(number: '1234') subject.stubs(fetch_credit_card: card) subject.card_number.should be == '1234' end end
create(:user) } it "returns the user's credit card" do card = CreditCard.new(number: '1234') subject.stubs(fetch_credit_card: card) subject.card_number.should be == '1234' end end
create(:user) } it "returns the user's credit card" do fake_card = stub(number: '1234') CreditCard.stubs(new: fake_card) subject.card_number.should be == '1234' end end
def fetch_credit_card credit_card_fetcher.new(user: self).find end def credit_card_fetcher @fetcher ||= CreditCard end def credit_card_fetcher=(fetcher_class) @fetcher = fetcher_class end end
def initialize(user); end def fetch CreditCard.new(number: '1234') end end subject { create(:user) } before { subject.credit_card_fetcher = FakeCreditCard } it "returns the user's card number" do subject.card_number.should be == '1234' end end
def initialize(user); end def fetch CreditCard.new(number: '1234') end end subject { create(:user) } before { subject.credit_card_fetcher = FakeCreditCard } it "returns the user's card number" do subject.card_number.should be == '1234' end end
en la mañana y no en la tarde Muy fácil crear tests que pasan ahora, pero no cuando cambia el DST. Muy fácil crear tests que pasan en tu computador, pero no en CI. Tiempo del servidor distinto al cliente: tests para Javascript se complican
article.publish! } it 'displays the published date and time' do assigns[:article] = article render response.should contain("Published at #{Time.now.to_s}") end . Finished in 0.1428 seconds 1 example, 0 failures
article.publish! } it 'displays the published date and time' do assigns[:article] = article render response.should contain("Published at #{Time.now.to_s}") end F Finished in 0.1234 seconds 1 example, 1 failure
} before do Timecop.freeze article.publish! end after { Timecop.return } it 'displays the published date and time' do assigns[:article] = article render response.should contain("Published at #{Time.now.to_s}") end . Finished in 0.0981 seconds 1 example, 0 failures
class Article def publish(time = Time.now) update_attributes(published: true, published_at: time) end end let(:article) { build(:article) } let(:publish_time) { Time.now } before { article.publish!(publish_time) } it 'displays the published date and time' do assigns[:article] = article render response.should contain("Published at #{publish_time.to_s}") end . Finished in 0.0981 seconds 1 example, 0 failures
en el cliente Ejemplo en Rails: <%= javascript_tag do -%> Date.currentDate = function() { <% if Rails.env.test? -%> return new Date(<%== Time.now.to_json %>); <% else -%> return new Date(); <% end -%> }; <% end -%> Luego ser juiciozo con siempre utilizar Date.currentDate() en lugar de new Date()
Cómo NO testearlo: make_call() sleep(5) check_response() Si el response es en 1 segundo, tu suite pierde 4 segundos esperando Peor aun, si el response es en 6 segundos, tienes un falso negativo.
side of the moon') create(:album, title: 'Dark Magus') create(:album, title: 'Jimi: Blues') end visit '/search' fill_in 'Search', with: 'Dark' click_button 'Submit' sleep(5) page.should have_content('Dark side of the moon') page.should have_content('Dark Magus') page.should have_no_content('Jimi: Blues')
moon') create(:album, title: 'Dark Magus') create(:album, title: 'Jimi: Blues') end visit '/search' fill_in 'Search', with: 'Dark' click_button 'Submit' wait_limit = 5 start_time = Time.now until done do raise TimeoutError if (Time.now - start_time) > wait_limit done = page.has_content?('Here are your results') sleep(0.1) end page.should have_content('Dark side of the moon')
de red Tests mas rapidos Puedes imponer el estado del servicio para testear edge cases Puedes testear comportamiento de tu sistema cuando hay timeouts en el servicio remoto Elimina fallas de tus tests causados por problemas en el servicio
line argument When I suspend a project called "test_project" with: | argument | value | | --heroku | true | Then the "test_project-production" heroku app should exist And the "test_project-production" heroku app should exist
line argument When I suspend a project called "test_project" with: | argument | value | | --heroku | true | Then the "test_project-production" heroku app should exist And the "test_project-production" heroku app should exist
File.open(RECORDER, 'a') do |file| file.write @args.join(' ') end end def self.clear! FileUtils.rm_rf(RECORDER) end def self.has_created_app?(app_name) File.open(RECORDER, 'r').read.include?("create #{app_name}") end end After do FakeHeroku.clear! end
File.open(RECORDER, 'a') do |file| file.write @args.join(' ') end end def self.clear! FileUtils.rm_rf(RECORDER) end def self.has_created_app?(app_name) File.open(RECORDER, 'r').read.include?("create #{app_name}") end end After do FakeHeroku.clear! end
File.open(RECORDER, 'a') do |file| file.write @args.join(' ') end end def self.clear! FileUtils.rm_rf(RECORDER) end def self.has_created_app?(app_name) File.open(RECORDER, 'r').read.include?("create #{app_name}") end end After do FakeHeroku.clear! end
File.open(RECORDER, 'a') do |file| file.write @args.join(' ') end end def self.clear! FileUtils.rm_rf(RECORDER) end def self.has_created_app?(app_name) File.open(RECORDER, 'r').read.include?("create #{app_name}") end # recuerda: # FakeHeroku.should have_created_app(app_name) end
true do it 'does stuff' do (2 + 2).should == 5 end end spec/spec_helper.rb Rspec.configure do |config| config.filter_run_excluding quarantine: true end > rspec --tags ~quarantine > rspec --tags quarantine