[英]Rails 3 Rspec Error - SQLite3::SQLException: no such column: comments.article_id
I am trying to test for a comment deletion using Rspec. 我正在尝试使用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: 这是我正在运行的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
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... 我显式设置@ comment.article_id = @ article.id,所以我不知道为什么会说不存在任何列...
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. 我可以通过运行
rake db:test:prepare
然后运行Rspec来解决它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.