dimanche 10 avril 2016

RSpec testing with followerships / friendships has many through users

I inspired myself with the following link, http://ift.tt/PjCZii, but the rspec testing is not coming easy.

user model:

class User < ActiveRecord::Base

  # Associations
  has_many :followerships
  has_many :followers, :through => :followerships
  has_many :inverse_followerships, :class_name => "Followership", :foreign_key => "follower_id"
  has_many :inverse_followers, :through => :inverse_followerships, :source => :user
end

followership model:

class Followership < ActiveRecord::Base
  belongs_to :user
  belongs_to :follower, :class_name => "User"
end

followerhip factory:

FactoryGirl.define do
  factory :followership do
    user_id 1
    follower_id 1
  end
end

followerships controller:

class FollowershipsController < InheritedResources::Base
  def create
    @followership = current_user.followerships.build(:follower_id => params[:follower_id])
    if @followership.save
      flash[:notice] = "Following."
      redirect_to root_url
    else
      flash[:error] = "Unable to follow."
      redirect_to root_url
    end
  end

  def destroy
    @followership = current_user.followerships.find(params[:id])
    @followership.destroy
    flash[:notice] = "Removed followership."
    redirect_to current_user
  end
end

folowerships controller spec (this is all wrong):

require 'rails_helper'

describe FollowershipsController do
  let(:followership) { create(:followership) }
  let(:follower) { followership.follower }
  let(:user) { create(:user) }

  before do
    sign_in :user, user
  end

  describe "#create" do
    it "saves the followership" do
      post :create, followership: { follower_id: follower }

      expect(response).to redirect_to(root_path)
      expect(assigns[:followership].followership.followers).to eq(user)
      expect(flash[:notice]).to eq("Following.")
    end

    it "fails to save followership" do
      expect(post :create, followership: { follower_id: nil }).to redirect_to(root_path)
      expect(flash[:notice]).to eq("Unable to follow.")
    end
  end

  describe "#destroy" do
    it "deletes the followership" do
      expect {
        delete :destroy, id: follower
      }.to change(Followership, :count).by(-1)

      expect(flash[:notice]).to eq("Removed followership.")
    end
  end

end

Error from followerships controller Rspec

FollowershipsController
  #destroy
    deletes the followership (FAILED - 1)
  #create
    saves the followership (FAILED - 2)
    fails to save followership (FAILED - 3)

Failures:

  1) FollowershipsController#destroy deletes the followership
     Failure/Error:
       expect {
         delete :destroy, id: follower
       }.to change(Followership, :count).by(-1)

       expected #count to have changed by -1, but was changed by 0

  2) FollowershipsController#create saves the followership
     Failure/Error: expect(assigns[:followership].followership.followers).to eq(user)

     NoMethodError:
       undefined method `followership' for #<Followership:0x00000105af2db8>

  3) FollowershipsController#create fails to save followership
     Failure/Error: expect(flash[:notice]).to eq("Unable to follow.")

       expected: "Unable to follow."
            got: "Following."

       (compared using ==)
        3 examples, 3 failures

Thanks for the help :)

Aucun commentaire:

Enregistrer un commentaire