2015-03-02 144 views
1

我在我的rspec中有以下代碼(目的是測試控制器功能是否正常工作)。控制器rspec不能正常工作

require 'rails_helper' 

describe CollaborationsController do 

include TestFactories 

before do 
    # @user = create(:user) 
    @user = authenticated_user 
    # @wiki = create(:wiki, user: @user) 
    @wiki = authenticated_wiki 
    sign_in @user 
end 

describe "#create" do 
    expect (@user.collaborations.find_by_wiki_id(@wiki.id)).to be_nil 
    post :create, {wiki_id: @wiki.id} 
    expect(@user.collaborations.find_by_wiki_id(@wiki.id)).not_to be_nil 
end 

describe "#destroy" do 
    expect (@user.collaborations).to eq (0) 
    post :create, {wiki_id: @wiki.id} 
    expect (@user.collaborations).to eq (1) 

    delete :destroy, {wiki_id: @wiki.id, id: collaborations.id} 
    expect(@user.collaborations.find_by_wiki_id(@wiki.id)).not_to be_nil 
end 

end 


def authenticated_user 
    user = User.new(email: "email#{rand}@fake.com", password: 'password') 
    user.skip_confirmation! 
    user.save 
    user 
end 


def authenticated_wiki 
    wiki = Wiki.new(title: "Dit is de title", body: 'password') 
    wiki.save 
    wiki 
end 

當我運行它,但我得到以下錯誤:

undefined method `collaborations' for nil:NilClass 

當我嘗試從控制檯的關係一切正常,這似乎有種奇怪,我的事業。

U = User.first u.collaborations給出了這樣的FE

collaboration Load (0.3ms) SELECT "collaborations".* FROM "collaborations" WHERE "collaborations"."user_id" = ? [["user_id", 1]] 
=> #<ActiveRecord::Associations::CollectionProxy [#<Collaboration id: 18,  user_id: 1, wiki_id: 1, created_at: "2015-02-26 08:54:36", updated_at: "2015-02-26 08:54:36">, #<Collaboration id: 19, user_id: 1, wiki_id: 1, created_at: "2015-02- 26 09:07:19", updated_at: "2015-02-26 09:07:19">]> 
2.1.5 :003 > 

在什麼即時做錯了任何最新的想法?

+0

你在無調用'collaborations'的地方在哪裏(應該有回溯到那個錯誤的地方),你會在一半的地方 – 2015-03-02 08:18:58

+0

Jes,我明白我正在調用nill的合作。不過,我可以在rails c中調用用戶的協作,所以錯誤應該在我的語法中? – user3706202 2015-03-02 08:37:08

+0

你的'describe'塊裏的'it'在哪裏? – hlcfan 2015-03-02 09:32:13

回答

0

嘗試使用before(:each)

此外,我想這樣做在let(:user){ authenticated_user }描述塊。

,做一個期望在一個it

1

恐怕你的代碼是無效的。您正在將測試代碼放在describe區塊內,但應該將其封裝爲it。嘗試格式化這樣的:

describe "#create" do 
    expect (@user.collaborations.find_by_wiki_id(@wiki.id)).to be_nil 
    post :create, {wiki_id: @wiki.id} 
    expect(@user.collaborations.find_by_wiki_id(@wiki.id)).not_to be_nil 
end 

describe "#destroy" do 
    expect (@user.collaborations).to eq (0) 
    post :create, {wiki_id: @wiki.id} 
    expect (@user.collaborations).to eq (1) 

    delete :destroy, {wiki_id: @wiki.id, id: collaborations.id} 
    expect(@user.collaborations.find_by_wiki_id(@wiki.id)).not_to be_nil 
end 

這樣的:

describe "#create" do 
    it "sets the collaborations" do 
    expect (@user.collaborations.find_by_wiki_id(@wiki.id)).to be_nil 
    post :create, {wiki_id: @wiki.id} 
    expect(@user.collaborations.find_by_wiki_id(@wiki.id)).not_to be_nil 
    do 
end 

describe "#destroy" do 
    it "destroys collaborator" do 
    expect (@user.collaborations).to eq (0) 
    post :create, {wiki_id: @wiki.id} 
    expect (@user.collaborations).to eq (1) 

    delete :destroy, {wiki_id: @wiki.id, id: collaborations.id} 
    expect(@user.collaborations.find_by_wiki_id(@wiki.id)).not_to be_nil 
    end 
end 

有時缺乏it的導致奇怪的行爲,和誤導性的錯誤消息。

希望這能解決您的問題!