![](/img/trans.png)
[英]RoR error: SQLite3::SQLException: no such column: comments.micropost_id: SELECT “comments”.* FROM “comments” WHERE “comments”.“micropost_id” = 2
[英]Rails 3 Rspec Error - SQLite3::SQLException: no such column: comments.article_id
我正在尝试使用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,所以我不知道为什么会说不存在任何列...
也许测试数据库不同步? 您是否尝试检查测试数据库中的架构以查看该列是否存在?
我在测试中遇到了类似的问题,因为该表不存在,因此无法获取页面。 我可以通过运行rake db:test:prepare
然后运行Rspec来解决它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.