[rspec-users] Problem testing method

Carlos Rafael Belizón Ibáñez paliyoes at gmail.com
Thu Sep 25 06:51:28 EDT 2008


El jue, 25-09-2008 a las 10:27 +0100, Matt Wynne escribió:
> You're expecting the Bar class to implement an interface which  
> supports you calling count -= 1 on it.
> 
> Have you checked (for example, using script/console) that Bar does  
> indeed offer this method? The error indicates that it doesn't, and  
> from the code you've shown us I'd be surprised if it did - it's not  
> something you can do to normal ActiveRecord objects - you'd have to  
> destroy a particular instance, for example, for the result of count  
> to go down.
> 
> I think it might be easier for us if you can show us an example  
> that's closer to what you're actually trying to do.
> 
> cheers,
> Matt
> ----
> http://blog.mattwynne.net
> http://songkick.com
> 
> In case you wondered: The opinions expressed in this email are my own  
> and do not necessarily reflect the views of any former, current or  
> future employers of mine.
> 
> 
> 
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Ok. I'm coding a web application called: SFO (Soccer Football Online).
This project it's like game Football Manager, but all players are
humans. Now, I'm coding the Class Alineado, here's the Model of this
Class:

# == Schema Information
# Schema version: 20080923152754
#
# Table name: alineados
#
#  id          :integer         not null, primary key
#  club_id     :integer         not null
#  jugador_id  :integer         not null
#  partido_id  :integer         not null
#  posicion    :integer         not null
#  entrada     :integer         
#  salida      :integer         
#  alineado_id :integer         
#

class Alineado < ActiveRecord::Base  #This class would be Foo.
  belongs_to :club
  has_many :jugadores
  belongs_to :partido
  has_one :alineado
  validates_presence_of :club_id, :jugador_id, :partido_id, :posicion
  
  
  def jugando?
    !entrada.nil? and entrada <= self.partido.minuto and salida.nil?
  end
  
  def sustituido?
    !salida.nil? and salida <= self.partido.minuto
  end
  
  def banquillo?
    entrada.nil? or sustituido? or !jugando?
  end
  
  def sustituido_por?
    self.alineado
  end
  
  def cambiar_por(jugador_entrante) #This would be the method foo of Foo
class
    cambio = false
    if jugador_entrante.banquillo? && !jugador_entrante.sustituido? &&
        jugador_entrante.club_id == self.club_id
      if club_id == self.partido.local 
        if self.partido.cambios_local > 0
          self.partido.cambios_local -= 1 #and this would be the
operation self.bar.count -= 1
          cambio = true
        end
      else #el jugador es del equipo visitante
        if self.partido.cambios_visitante > 0
          self.partido.cambios_visitante -= 1 #same operation
          cambio = true
        end
      end
    end
    
    if cambio
      self.salida = self.partido.minuto
      jugador_entrante.entrada = self.partido.minuto
    end
    
    cambio
  end
  
  def validate
    errors.add(:entrada, "no debe ser menor que 0 si está jugando") if !
entrada.nil? and entrada < 0
    errors.add(:entrada, "no debe ser mayor que el minuto actual") if !
entrada.nil? and self.entrada > self.partido.minuto
    errors.add(:entrada, "no debe tener una entrada mayor o igual que la
salida al partido") if !entrada.nil?  and !salida.nil? and entrada >=
salida
    errors.add(:salida, "no debe haber sustituido si no ha entrado al
partido") if entrada.nil? and !salida.nil?
  end
end

#partido.rb 
Class Partido < ActiveRecord::Base #This would be the class Bar
end

#alineado_spec.rb
module AlineadoSpecHelper
  def atributos_validos(partido=nil, jugador=1, club=1, posicion=1)
    {
      :club_id => club,
      :jugador_id => jugador,
      :partido => partido,
      :posicion => posicion
    }
  end
end

describe Alineado, ".cambiar_por" do
  include AlineadoSpecHelper
  
  before(:each) do
    @partido = mock(Partido)
    @partido.stub!('local').and_return(:equipo_local)
    @partido.stub!('visitante').and_return(:equipo_visitante)
    @partido.stub!('minuto').and_return(20)
  end
  
  describe "with the game in play and sustitutions aviable" do
    before(:each) do
      @partido.stub!('cambios_local').and_return(3)
      @partido.stub!('cambios_visitante').and_return(3)
      @jugador_sustituido = Alineado.new(atributos_validos(@partido, 
          :jugador_sustituido, :equipo_local))
      @jugador_sustituido.entrada = 0
      @jugador_entrante = Alineado.new(atributos_validos(@partido, 
          :jugador_entrante, :equipo_local))
      @cambios_antes = @partido.cambios_local
    end

    it "should decrement in one the sustitutions aviables" do
      @jugador_sustituido.cambiar_por(@jugador_entrante)
      @partido.cambios_local.should eql(@cambios_antes - 1)
    end
end

And I got this error:

4)
Spec::Mocks::MockExpectationError in 'Alineado.cambiar_por with the game
in play and sustitutions aviable should decrement in one the
sustitutions aviables'
Mock 'Partido_1004' received unexpected message :cambios_visitante= with
(2)
/home/carlos/NetBeansProjects/ofs/app/models/alineado.rb:53:in
`cambiar_por'
spec/models/alineado_spec.rb:263:
spec/models/alineado_spec.rb:193:

And I don't know how fix this error. It's possible test this?

P.D.: Sorry for my english.



More information about the rspec-users mailing list