2016-04-10 3 views
1

Ich inspirierte mich mit dem folgenden Link, http://railscasts.com/episodes/163-self-referential-association, aber die RSPE Prüfung wird nicht einfach.RSpec Tests mit Anhänger/Freundschaften hat viele durch Benutzer

Benutzermodell:

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 Modell:

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

followerhip Fabrik:

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-Spezifikation (das ist alles falsch):

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: follower }).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 

Fehler von 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: delete :destroy, id: follower 

    ActionController::UrlGenerationError: 
     No route matches {:action=>"destroy", :controller=>"followerships", :id=>nil} 

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

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

    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 ==) 

Danke für die Hilfe:)

Antwort

2

Der Befehl let verwendet eine faule Auswertung, daher werden diese Datensätze erst erstellt, wenn sie aufgerufen werden. Verwenden Sie die let! Syntax, um sicherzustellen, sie erstellt, bevor die Tests ausgeführt werden:

let!(:followership) { create(:followership) } 
    let!(:follower) { followership.follower } 
    let!(:user) { create(:user) } 

Stellen Sie sicher, dass Validierungen auch nur Schaffung eines folgenden ermöglichen, wenn sie nicht bereits für dieses Paar von Benutzer vorhanden sind:

class Followership < ActiveRecord::Base 
    validates_uniqueness_of :user_id, scope: :follower_id 

auch ist es nicht garantiert, dass die follower/followership Beziehungen zu user seit user gehören, werden nicht unbedingt eine id von 1.

Schließlich ist assigns eine Methode, so sollte die Syntax assigns(:followership) nicht assigns[:followership]

+0

Hallo Anthony sein, ich habe Ihre Kommentare akzeptiert. Ich weiß, dass die Fabrik und die Rspec nicht funktionieren ... irgendwelche Ideen? Vielen Dank – Jony