简体   繁体   中英

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

I am trying to test for a comment deletion using Rspec. I think my test is wrong because when I try to delete a comment in my browser, it works.

Here is the Rspec test that I am running:

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

Here is the error I get:

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

Here is the destroy portion of my comments controller:

  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

Here's the create portion of my comments controller:

  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

I explicitly set @comment.article_id = @article.id, so I don't know why it would say that there is no column that exists...

Maybe the test database is out of sync? Did you try checking the schema in the test db to see if the column is there?

I was having a similar problem with my tests showing that I couldn't GET a page because the table didn't exist. I was able to resolve it by running rake db:test:prepare and then running Rspec.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM