Last active
August 29, 2015 13:55
-
-
Save joelbrewer/8732582 to your computer and use it in GitHub Desktop.
projects_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe ProjectsController do | |
let(:user) { FactoryGirl.create(:user) } | |
describe "GET #index" do | |
context "when user is not signed in" do | |
it "redirects to sign up page" do | |
get :index | |
response.should redirect_to new_user_session_url | |
end | |
end | |
context "when user is signed in" do | |
let(:project) { FactoryGirl.create(:project, user: user, category: "clothing") } | |
before do | |
sign_in user | |
FactoryGirl.create(:plan_with_subscription, user: user) | |
end | |
it "assigns all projects to @projects" do | |
get :index | |
assigns(:projects).should include(project) | |
end | |
it "filters out projects based on search terms" do | |
get :index, q: {category_cont: "clothing"} | |
get :index, q: {category_cont: "clothing"}, commit: "search" | |
assigns(:projects).should_not include(project) | |
end | |
end | |
end | |
end | |
class ProjectsController < ApplicationController | |
before_filter :authenticate_user! | |
before_action :correct_user, only: :destroy | |
def create | |
@project = current_user.projects.build(project_params) | |
if @project.save | |
flash[:success] = "Project created!" | |
redirect_to profile_path | |
else | |
render new_project_path | |
end | |
end | |
def index | |
binding.pry | |
@search = Project.search(params[:q]) | |
@projects = @search.result | |
end | |
def edit | |
@project = current_user.projects.find_by(id: params[:id]) | |
end | |
def new | |
@project = current_user.projects.build | |
@categories = ProjectCategory.all | |
end | |
def update | |
@project= Project.find_by_id(params[:id]) | |
if @project.update_attributes(project_params) | |
flash[:success] = "Project updated" | |
redirect_to profile_path | |
else | |
render 'edit' | |
end | |
end | |
def destroy | |
@project.destroy | |
redirect_to profile_path | |
end | |
private | |
def correct_user | |
@project = current_user.projects.find_by(id: params[:id]) | |
rescue | |
redirect_to profile_path | |
end | |
def project_params | |
params.require(:project).permit( | |
:title, | |
:contact_name, | |
:email_address, | |
:phone_number, | |
:description, | |
:category, | |
:sub_category) | |
end | |
end | |
Failures: | |
1) ProjectsController GET #index when user is signed in filters out projects based on search terms | |
Failure/Error: assigns(:projects).should_not include(project) | |
expected #<ActiveRecord::Relation [#<Project id: 40, title: "Sample Project", created_at: "2014-01-31 14:45:38", updated_at: "2014-01-31 14:45:38", user_id: 50, contact_name: "Jay Z", email_address: "jay@z.com", phone_number: "123-456-7890", description: "Best project ever", category: "clothing", sub_category: "record studio">]> not to include #<Project id: 40, title: "Sample Project", created_at: "2014-01-31 14:45:38", updated_at: "2014-01-31 14:45:38", user_id: 50, contact_name: "Jay Z", email_address: "jay@z.com", phone_number: "123-456-7890", description: "Best project ever", category: "clothing", sub_category: "record studio"> | |
Diff: | |
# ./spec/controllers/projects_controller_spec.rb:36:in `block (4 levels) in <top (required)>' | |
Finished in 15.56 seconds | |
3 examples, 1 failure | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment