I have some model with the following validation: validates_inclusion_of :value, :in => I18n.available_locales.map(&:to_s) I would think that I would be able to stub out that method in a test like so: it "should accepted any available locale as a value" do I18n.stub!(:available_locales).and_return([''abc'']) my_model = MyModel.new( :value => ''abc'' ) my_model.should have(:no).errors_on(:value) end But it keeps failing. It looks like the I18n inside the model is not being stubbed correctly?!?! What am I doing wrong?
When you spec runs (and your stub! is called) the method has already been called, so your stub will never catch anything. - Maur?cio Linhares http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr On Thu, Aug 20, 2009 at 5:03 PM, BallaBall<balla at sogetthis.com> wrote:> I have some model with the following validation: > > ?validates_inclusion_of :value, > ? ?:in => I18n.available_locales.map(&:to_s) > > I would think that I would be able to stub out that method in a test > like so: > > it "should accepted any available locale as a value" do > ? ? ?I18n.stub!(:available_locales).and_return([''abc'']) > > ? ? ?my_model = MyModel.new( :value => ''abc'' ) > > ? ? ?my_model.should have(:no).errors_on(:value) > end > > But it keeps failing. It looks like the I18n inside the model is not > being stubbed correctly?!?! What am I doing wrong? > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Ohhh so when the model is first loaded it has already run the method im trying to stub? On Aug 20, 2:00?pm, Maur?cio Linhares <mauricio.linha... at gmail.com> wrote:> When you spec runs (and your stub! is called) the method has already > been called, so your stub will never catch anything. > > - > Maur?cio Linhareshttp://codeshooter.wordpress.com/|http://twitter.com/mauriciojr > > > > On Thu, Aug 20, 2009 at 5:03 PM, BallaBall<ba... at sogetthis.com> wrote: > > I have some model with the following validation: > > > ?validates_inclusion_of :value, > > ? ?:in => I18n.available_locales.map(&:to_s) > > > I would think that I would be able to stub out that method in a test > > like so: > > > it "should accepted any available locale as a value" do > > ? ? ?I18n.stub!(:available_locales).and_return([''abc'']) > > > ? ? ?my_model = MyModel.new( :value => ''abc'' ) > > > ? ? ?my_model.should have(:no).errors_on(:value) > > end > > > But it keeps failing. It looks like the I18n inside the model is not > > being stubbed correctly?!?! What am I doing wrong? > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
Yes, so it''s useless trying to stub it. Tools like Remarkable will let you spec that validates call in a better way -> http://github.com/carlosbrando/remarkable/ - Maur?cio Linhares http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr On Thu, Aug 20, 2009 at 6:07 PM, BallaBall<balla at sogetthis.com> wrote:> Ohhh so when the model is first loaded it has already run the method > im trying to stub? >