Search code examples
ruby-on-railsrubyrspec-rails

Best practices if lambdas are triggered by rspec


I created a controller action like this:

def update
  manager.update(
    price_change: -> {
      redirect_to new_painting_order_path(painting)
    },
    price_not_change: -> {
      user_path(current_user)
    }
  )
end

It works, but I have a problem in my specs. I want to check if the lambda is correctly called if the price changes or if it does not change. I stubbed the manager object. I can check which params are passed, but I think it's difficult to check lambdas. In addition, I want to check if actions in lambdas are correct.

Are there best practices to do something like this?


Solution

  • What is :price_change returning? Could you not write this as an if statement?

    def update
      if manager.update_attributes(params[:manager])
        if price_change?
          redirect_to new_painting_order_path(painting)
        else
          user_path(current_user)
        end
      end
    end