我想写一个测试以确保“专家”用户可以创建文章,而“基本”用户不能。例如,基本用户不能去这里:"http://0.0.0.0:3000/articles/new"
。以下是我的文章控制器的缩写版本,后面是文章测试。控制器工作,但我希望测试证明它。我不知道该在什么地方说“代码在这里”。谢谢。Rails rspec测试控制器cancan功能
articles_controller:
class ArticlesController < ApplicationController
load_and_authorize_resource
# GET /articles/new
# GET /articles/new.json
def new
puts "in articles new"
@article = Article.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @article }
end
end
end
articles_controller_spec:
describe ArticlesController do
before (:each) do
@user = FactoryGirl.create(:user)
@user.role = "basic"
sign_in @user
end
describe "create article" do
it "should not create new article" do
#code goes here
end
end
end
我其实是想决定是否在模型或控制器规格中测试我的能力。你能解释为什么你认为我在模型规范中测试是首选方法吗? – Emeka
速度更快,需要更少的代码才能覆盖大部分情况,并且所有测试都在同一个地方。 – ck3g
在我的第10个规范试图从控制器中解析json之后,我想我开始同意了。根据能力,测试比整个控制器的能力要容易得多。 – Emeka