I am trying to run a test of my app buy my
relationships_controller_spec.rb file won’t run. Here’s the error I
get:
Exception encountered: #<NameError: uninitialized constant
RelationshipsController>
Here’s the code for my relationships_controller_spec.rb file:
require ‘spec_helper’
describe RelationshipsController do
describe “access control” do
it "should require signin for create" do
post :create
response.should redirect_to(signin_path)
end
it "should require signin for destroy" do
delete :destroy, :id => 1
response.should redirect_to(signin_path)
end
end
describe “POST ‘create’” do
before(:each) do
@user = test_sign_in(Factory(:user))
@followed = Factory(:user, :email => Factory.next(:email))
end
it "should create a relationship" do
lambda do
post :create, :relationship => { :followed_id => @followed }
response.should be_redirect
end.should change(Relationship, :count).by(1)
end
it "should create a relationship using Ajax" do
lamda do
xhr :post, :create, :relationship => { :followed_id =>
@followed }
response.should be_success
end.should change(Relationship, :count).by(1)
end
end
describe “DELETE ‘destroy’” do
before(:each) do
@user = test_sign_in(Factory(:user))
@followed = Factory(:user, :email => Factory.next(:email))
@user.follow!(@followed)
@relationship =
@user.relationships.find_by_followed_id(@followed)
end
it "should destroy a relationship" do
lambda do
delete :destroy, :id => @relationship
response.should be_redirect
end.should change(Relationship, :count).by(-1)
end
it "should destroy a relationship using Ajax" do
lambda do
xhr :delete, :destroy, :id => @relationship
response.should be_success
end.should change(Relationship, :count).by(-1)
end
end
end
Here’s the code for my relationships_controller.rb
class RelationshipsController < ApplicationController
before_filter :authenticate
def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
end
And here’s the code for my relationship model (relationship.rb):
class Relationship < ActiveRecord::Base
attr_accessible :followed_id
belongs_to :follower, :class_name => “User”
belongs_to :followed, :class_name => “User”
validates :follower_id, :presence => true
validates :followed_id, :presence => true
end