views:

69

answers:

1

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 => "[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

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...

A: 

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?

zetetic