簡體   English   中英

Rails 3 Rspec錯誤-SQLite3 :: SQLException:沒有這樣的列:comments.article_id

[英]Rails 3 Rspec Error - SQLite3::SQLException: no such column: comments.article_id

我正在嘗試使用Rspec測試注釋刪除。 我認為我的測試是錯誤的,因為當我嘗試在瀏覽器中刪除評論時,它可以工作。

這是我正在運行的Rspec測試:

before(:each) do
    @admin = Factory(:user, :admin => true)
    @user = Factory(:user, :email => "example1@example.com")
    @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,所以我不知道為什么會說不存在任何列...

也許測試數據庫不同步? 您是否嘗試檢查測試數據庫中的架構以查看該列是否存在?

我在測試中遇到了類似的問題,因為該表不存在,因此無法獲取頁面。 我可以通過運行rake db:test:prepare然后運行Rspec來解決它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM