2010-09-07 85 views
0

我想使用Rspec測試註釋刪除。我認爲我的測試是錯誤的,因爲當我嘗試在瀏覽器中刪除評論時,它會起作用。Rails 3 Rspec錯誤 - SQLite3 :: SQLException:沒有這樣的列:comments.article_id

這裏是Rspec的測試,我運行:

before(:each) do 
    @admin = Factory(:user, :admin => true) 
    @user = Factory(:user, :email => "[email protected]") 
    @article = Factory(:article, :user => @user, :title => "Article Title", :content => "Article Content") 
    @comment = Factory(:comment, :user => @user, :article => @article, :title => "Comment Title", :content => "Comment Content") 
    end 

    it "should allow access to 'destroy'" do 
    lambda do 
     delete :destroy, :id => @comment, :article_id => @article 
    end.should change(Comment, :count).by(-1) 
    end 

這裏是我的錯誤:

1) CommentsController access control for admin should allow access to 'destroy' 
    Failure/Error: delete :destroy, :id => @comment, :article_id => @article 
    SQLite3::SQLException: no such column: comments.article_id: SELECT  "comments"."id", "comments"."parent_id", "comments"."title", "comments"."content", "comments"."user_id", "comments"."created_at", "comments"."updated_at" FROM  "comments" WHERE  ("comments".article_id = 1) AND ("comments"."id" = 1) ORDER BY comments.created_at DESC LIMIT 1 

這裏是我的意見控制器的破壞部分:

def destroy 
    @article = Article.find(params[:article_id]) 
    if @article.comments.find(params[:id]).destroy 
     flash[:success] = "Comment deleted." 
    else 
     flash[:error] = "Comment could not be deleted." 
    end 
    redirect_to @article 
    end 

下面是我的評論控制器的創建部分:

def create 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.build(params[:comment]) 
    @comment.user_id = current_user.id 
    @comment.article_id = @article.id 
    if @comment.save 
     flash[:success] = "Comment saved." 
     redirect_to @article 
    else 
     flash[:error] = "Error in creating comment." 
     @comments = @article.comments.paginate(:page => params[:page]) 
     render 'articles/show' 
    end 
    end 

我明確設置@ comment.article_id = @ article.id,所以我不知道爲什麼它會說,沒有列存在......

回答

0

也許測試數據庫超出同步?你有沒有嘗試檢查測試數據庫中的模式以查看列是否在那裏?

0

我有一個類似的問題,我的測試顯示我無法獲取頁面,因爲該表不存在。我能夠通過運行rake db:test:prepare然後運行Rspec來解決它。

相關問題