ruby on rails - Rspec test public controller method passing params to private controller method -
this controller:
class planscontroller def use_new_card @user = user.find_by_id(new_card_params[:user_id]) if new_stripe_card ... end private def new_card_params params.permit(:user_id, :stripetoken) end def new_stripe_card stripeservice.new({user_id: @user.id, customer_id: @user.stripe_customer_id, source: new_card_params[:stripetoken]}).new_card end end
i'm trying write controller spec tests when proper parameters post
use_new_card
method, new_stripe_card
's stripeservice.new
gets these parameters appropriately.
my spec looks far:
describe "when proper params passed" before @user = user.create(...) end "should allow stripeservice.new receive proper parameters" stripeservice.should_receive(:new).with({user_id: @user.id, customer_id: @user.stripe_customer_id, source: "test"}) post :use_new_card, user_id: @user.id, stripetoken: "test" end end
but i'm getting
failure/error: post :use_new_card, user_id: @user.id, stripetoken: "test" nomethoderror: undefined method `new_card' nil:nilclass
totally fair, i'm not sure how fix this... can't stub new_card
because stubbed method on nil
object still throw error (i tried adding stripeservice.any_instance.stub(:new_card).and_return(true)
before
block)
stubbed methods return nil
default. use and_return
specify value returned stub::
stripeservice.should_receive(:new).and_return(whatever)
or using newer syntax
expect(stripeservice).to receive(:new).and_return(whatever)
edit
pardon hand-waving. stub must return object act instance of stripeservice
extent required purposes of test. example:
let(:new_card) { double } let(:new_stripe_service) { double(new_card: new_card) } ... expect(stripeservice).to receive(:new).and_return(new_stripe_service)
now when test refers new_stripe_service
, rspec return test double has method stub named #new_card
, double. can add whatever additional stubs need make test pass. alternatively, can return actual instance of stripeservice
new_stripe_service
.
Comments
Post a Comment