Come posso aggiungere più aspettative should_receive su un oggetto usando RSpec?

StackOverflow https://stackoverflow.com/questions/244009

  •  04-07-2019
  •  | 
  •  

Domanda

Nel mio controller Rails, sto creando più istanze della stessa classe di modello. Voglio aggiungere alcune aspettative RSpec in modo da poter provare che sta creando il numero corretto con i parametri corretti. Quindi, ecco cosa ho nelle mie specifiche:

Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => @user.id, :position_id => 1, :is_leader => true)
Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => "2222", :position_id => 2)
Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => "3333", :position_id => 3)
Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => "4444", :position_id => 4)

Questo sta causando problemi perché sembra che la classe Bandmate possa avere solo 1 " should_receive " aspettativa impostata su di esso. Quindi, quando eseguo l'esempio, ottengo il seguente errore:

Spec::Mocks::MockExpectationError in 'BandsController should create all the bandmates when created'
Mock 'Class' expected :create with ({:band_id=>1014, :user_id=>999, :position_id=>1, :is_leader=>true}) but received it with ({:band_id=>1014, :user_id=>"2222", :position_id=>"2"})

Questi sono i parametri corretti per la seconda chiamata da creare, ma RSpec sta testando i parametri sbagliati.

Qualcuno sa come posso impostare le mie aspettative should_rece per consentire più chiamate diverse?

È stato utile?

Soluzione

Le aspettative multiple non sono affatto un problema. Quello che stai incontrando sono problemi di ordinazione, dati i tuoi argomenti specifici sulle aspettative non ordinate. Controlla questa pagina per dettagli sulle aspettative di ordinazione.

Il racconto è che dovresti aggiungere .ordered alla fine di ciascuna delle tue aspettative.

Altri suggerimenti

Conti di ricezione falsi

my_mock.should_receive (: MGS) .Una volta
  my_mock.should_receive (: MGS) .twice
  my_mock.should_receive (: SYM) .exactly (n) X
  my_mock.should_receive (: SYM) .at_least (: una volta)
  my_mock.should_receive (: SYM) .at_least (: due volte)
  my_mock.should_receive (: SYM) .at_least (n) X
  my_mock.should_receive (: SYM) .at_most (: una volta)
  my_mock.should_receive (: SYM) .at_most (: due volte)
  my_mock.should_receive (: SYM) .at_most (n) X
  my_mock.should_receive (: sym) .any_number_of_times

Funziona anche per rspec 2.5.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top