dimanche 24 mai 2015

How to test my rails controller with rspec without executing the methods?

This is how my controller looks like:

module ApiModule
class ProjectsController < ApplicationController

 def index

   render json: ApiModule::Project.all
   end

   def create
    project = ApiModule::Project.new(project_params)
    if project.save
      render json: project
    else
      render json: project.errors.full_messages, status: 422
    end
  end

  def show
    render json: ApiModule::Project.find(params[:id])
  end

  def update
    render json: ApiModule::Project.find(params[:id]).update_attributes!(project_params)
  end

  def destroy
    unless ApiModule::Project.destroy(params[:id])
      render json: { :success => false}
    end
  end
   .
   .
   .

I want to see how to test each method (index, create and ...) without actually executing it(just want to test the body) and don't actually make a call to the database. I am getting the data from an external database and as you can see I am rendering it out! On the other hand I want to test this with the Rspec. I read something about mock and stub methods but I dont know if that is the right way to approach the problem! Can someone please explain me clearly how to do this? Thanks

Aucun commentaire:

Enregistrer un commentaire