Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec assigning local variable?

Just wondering if there's a way to access local variables in rspec without turning them into instance variables? To explain my issue:

I have the following action:

  def queue_due_mail
    payments = Payment.due_soon.where(:send_reminder => true)
    payments.each do |p|
      PaymentMailer.delay.reminder_email(p)
      p.send_reminder = false
      p.save
    end
    redirect_to root_path
  end

And, in my spec, I want to run something like this:

it "should assign nearly due payments to payments" do
  Payment.stub_chain(:due_soon, :where) { [mock_payment] }
  get :queue_due_mail
  assigns[:payments].should eq([mock_payment])
end

The problem is that the assigns[:payments] call only works if I turn the local variable 'payments' into '@payments'. This isn't a major problem, but I'd rather not have my rspec tests influence by actual code.

So, is there a way to reference local variables in rspec assigns?

Cheers...

like image 775
PlankTon Avatar asked Nov 03 '11 13:11

PlankTon


1 Answers

basically no. Think of it as black-box testing. rspec tests what goes into or out of a method (a controller action is a method). Any local variables should be thrown away at the end of the method - therefore can't be tested when the method-call is over.

like image 115
Taryn East Avatar answered Oct 22 '22 16:10

Taryn East