Trying to build a controller spec using rspec on a rails controller I have named "API::ZipsController".
The controller accepts a parameter called "zip_code" and returns an active record model and 200 status.
However, when I run rspec the test fails with NoMethodError: undefined method 'where' for Zip:Module. I believe this is because 'where' is an instance method, not a class method so I will need to stub/mock it? Unfortunately I have not had any luck in resolving the problem as of yet.
zips_controller_spec.rb:
describe API::ZipsController do
require 'spec_helper'
it "returns 200 status with zones data" do
get 'show', zip_code: "V9A1B2"
response.should be_success
end
end
api/zips_controller.rb - (working):
class API::ZipsController < ApplicationController
def show
zip = Zip.where(zip_code: params[:zip_code].upcase.slice(0..2))
render json: zip, status: 200
end
end
zip.rb (model):
class Zip < ActiveRecord::Base
def as_json(options)
super(:only => [:zip_code])
end
end