Rails 3 Rspec错误 - SQLite3 :: SQLException:没有这样的列:comments.article_id

时间:2010-09-07 01:06:04

标签: ruby-on-rails rspec ruby-on-rails-plugins

我正在尝试使用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,所以我不知道为什么它会说没有列存在......

2 个答案:

答案 0 :(得分:0)

也许测试数据库不同步?您是否尝试检查测试数据库中的模式以查看该列是否存在?

答案 1 :(得分:0)

我的测试出现了类似的问题,显示我无法获取页面,因为该表不存在。我能够通过运行rake db:test:prepare然后运行Rspec来解决它。