Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

ruby on rails - Stub .push method in rspec

I have some code that uses .push. I want to write a unit test that tests the failure scenario of this code, but i can't figure out the syntax.

My code looks like this

   if object.groups.push(group)
    flash[:notice] = I18n.t(".success")
  else
    flash[:alert] = I18n.t(".failure")
  end

I've tried to stub out the .push method so it returns false, but it still returns true.

My stubbing attempts are below:

      object.groups.should_receive(:push).with(group).and_return(false)
  allow(object.groups).to receive(:push).with(group).and_call_original

Am i going about this the wrong way?

question from:https://stackoverflow.com/questions/65829913/stub-push-method-in-rspec

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...