Rails Rspec控制器Cancan功能的测试 [英] Rails rspec test for controller cancan abilities

查看:110
本文介绍了Rails Rspec控制器Cancan功能的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个测试来确保专家用户可以创建文章,而基本用户不能。例如,基本用户不能转到此处: http://0.0.0.0:3000/articles/new 。下面是我的Articles Controller的简化版本,然后是Articles测试。控制器可以工作,但是我想通过测试证明这一点。我不确定在代码在这里的地方写什么。谢谢。

I would like to write a test to make sure that "expert" users can create articles, and "basic" users cannot. For example, basic user cannot go here: "http://0.0.0.0:3000/articles/new". Below is a shortened version of my articles controller, followed by the articles test. The controller works, but I would like the test to prove it out. I'm not sure what to put where it says "code goes here". Thanks.

articles_controller:

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:

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


推荐答案

根据控制器规格测试CanCan能力

Testing CanCan abilities from your Controllers' specs will blow up your specs soon.

我更喜欢在 spec / models / ability_spec.rb = nofollow>可以/匹配器

I prefer to test abilities in spec/models/ability_spec.rb using cancan/matchers

这篇关于Rails Rspec控制器Cancan功能的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆