如何将哈希方法rspec传递给控制器

时间:2018-01-24 04:34:26

标签: ruby-on-rails ruby methods hash rspec

使用Rails 5.1.4,Ruby 2.4.1,rspec

情景:

在文章中,只允许用户current_ma_user使用角色“a,m”

然后:

检查current_ma_user.role =“a,m”    或current_ma_user自己的文章(@ article.user)

所以我创建current_ma_user作为哈希以及用户。 然后调用角色来检查什么是用户[角色]

问题:

  1. 如何添加新方法进行哈希。

  2. 如何将该hash.method从rspec controller_spec传递给控制器​​。

  3. 故障:

      1) ArticlesController DELETE #destroy destroys the requested article
         Failure/Error: delete :destroy, params: {id: article.to_param}, session: valid_session, :current_ma_user.role => "a,m"
    
         NoMethodError:
           undefined method `role' for :current_ma_user:Symbol
         # ./spec/controllers/articles_controller_spec.rb:172:in `block (4 levels) in <top (required)>'
         # ./spec/controllers/articles_controller_spec.rb:171:in `block (3 levels) in <top (required)>'
    

    这是gist

    articles_controller_spec.rb:

    require 'rails_helper'
    RSpec.describe ArticlesController, type: :controller do
    
        class Hash #patch to temp pass problem 1
          def role
            "a,m"  #Hard Code, need to call user["role"] need code
          end
        end
    
        user = {}
        user["uid"] = "admin"
        user["provider"] = "Facebook"
        user["email"] = "1.0@kul.asia"
        user["role"] = "a,m"
    
        current_ma_user = user
    
      describe "DELETE #destroy" do
    
        it "destroys the requested article" do
          article = Article.create! valid_attributes
          expect {
            delete :destroy, params: {id: article.to_param}, session: valid_session
          }.to change(Article, :count).by(-1)
        end
    
        it "redirects to the articles list" do
          article = Article.create! valid_attributes
          delete :destroy, params: {id: article.to_param}, session: valid_session
          expect(response).to redirect_to(articles_url)
        end
      end
    end
    

    控制器:

    class ArticlesController < ApplicationController   before_action :load_article, only: [:show, :destroy]
    
      def destroy
        if current_ma_user.role.upcase.split(',').include?("A") || current_ma_user == @article.user
        #if current_ma_user == @article.user
          @article.destroy
        end
          redirect_to :action=>'index'   end
    
      private
    
      def load_article
        @article = Article.find(params[:id])   end
    
    end
    

    更新了行号: enter image description here

    更新了调试,以显示.spec和控制器中current_ma_user的值 in spec

    in controller

1 个答案:

答案 0 :(得分:3)

这是您的错误来自(在您的控制器中):

if current_ma_user.role.upcase.split(',').include?("A") || current_ma_user == @article.user

建议的解决方案

  • 控制器中定义的current_ma_user在哪里?(如果未分配,则需要在之前分配,然后在current_ma_user变量上调用role方法。

试一试,看看它是怎么回事。

  • 做这样的事情:

current_ma_user = User.find( params[:user_id])

  • 现在你似乎想把一些东西传递给params哈希。请记住白名单,无论你决定传递给params。无论是用户ID还是角色ID等,还是角色字符串。

  • 编写测试时,将approrpiate值传递给params哈希。如果您在测试中传入user_id,则必须确保在测试中创建了用户。

delete :destroy, {:id => article.id.to_s, :user_id => @current_ma_user.id }, session: valid_session

  • 也许在您的spec文件中,在您的测试中,将current_ma_user放在before filter中并使其成为实例变量,以便所有测试都可以访问它:

    before(:each) do @current_ma_user = user.create( <--- create the user with the appropriate attributes here --->) end

警告:未经测试

我只是将它输入堆栈溢出编辑器。