【问题标题】:Rails 3 Rspec Error - SQLite3::SQLException: no such column: comments.article_idRails 3 Rspec 错误 - SQLite3::SQLException:没有这样的列:comments.article_id
【发布时间】:2010-09-07 01:06:03
【问题描述】:

我正在尝试使用 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

这是我的 cmets 控制器的销毁部分:

  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

这是我的 cmets 控制器的创建部分:

  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,所以不知道为什么会说不存在列...

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-22
        • 2016-01-19
        • 2022-08-15
        • 2013-10-09
        • 1970-01-01
        • 2015-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多