Ruby on Rails 控制器测试

派蒙55

我的测试有问题。我使用脚手架来创建我的应用程序,但在我的控制器中改变了一些东西。我的第一个控制器如下所示

 class ChildrenController < ApplicationController
   before_action :set_child, only: [:show, :edit, :update, :destroy]

 require 'httparty'
 #  require 'open-uri'
  include HTTParty
  # include I18n


 def child_admin
   @children = Child.all
 end

   # GET /children
   # GET /children.json
   def index
     @children = Child.all

   end

   # GET /children/1
   # GET /children/1.json
   def show
     @child = Child.find(params[:id])
     authorize! :read, @child
   end

  # GET /children/new
   def new
     @child = Child.new
   end

  # GET /children/1/edit
   def edit
   end

   # POST /children
   # POST /children.json
   def create
     @kigas = Kiga.all
     @relations = Relation.all
     @child = Child.new child_params
     @child.user = current_user

     url = "https://maps.googleapis.com/maps/api/geocode/json?address=#{I18n.transliterate(@child.city)}+#{I18n.transliterate(@child.streed)}+#{@child.add_number}&key=AIzaSyBWwoVj5WQMN9-Ij7IJWxQL1CzuigzBsYc"
latlongchild = HTTParty.get(url)
     # puts latlongchild
     # puts latlongchild["results"].first["geometry"]["location"]
     childlat=latlongchild["results"].first["geometry"]["location"]["lat"]
     childlng=latlongchild["results"].first["geometry"]["location"]["lng"]
   #  @child.save
     respond_to do |format|
       if @child.save
         format.html { redirect_to @child, notice: 'Kind wurde erfolgreich registriert.' }
         format.json { render :show, status: :created, location: @child }
       else
         format.html { render :new }
         format.json { render json: @child.errors, status: :unprocessable_entity }
       end
     end
     @kigas.each do |kiga|
       url2 = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=#{I18n.transliterate(@child.city)}+#{I18n.transliterate(@child.streed)}+#{@child.add_number}&destinations=#{I18n.transliterate(kiga.city)}+#{I18n.transliterate(kiga.streed)}+#{kiga.add_number}&key=AIzaSyDjh9hMXm_lnIyaj_HLQpGvDcDasLjyhxk"
       response = HTTParty.get(url2)
       mesponse=response["rows"].first["elements"].first["distance"]["value"]
            url3 = "https://maps.googleapis.com/maps/api/geocode/json?address=#{I18n.transliterate(kiga.city)}+#{I18n.transliterate(kiga.streed)}+#{kiga.add_number}&key=AIzaSyBWwoVj5WQMN9-Ij7IJWxQL1CzuigzBsYc"
            response2 = HTTParty.get(url3)
            kigalat=response2["results"].first["geometry"]["location"]["lat"]
            kigalng=response2["results"].first["geometry"]["location"]["lng"]
            # puts mesponse
            # puts mysponse
            # puts @child.id
           # puts kiga.id
       @relation = Relation.new relation_params
       @relation.child_id = @child.id
       @relation.kiga_id = kiga.id
       @relation.distance = mesponse
       @relation.kigalat = kigalat
       @relation.kigalong = kigalng
       @relation.childlat = childlat
       @relation.childlong = childlng
       @relation.user_id = @child.user_id
       @relation.save
     end
   end

   # PATCH/PUT /children/1
   # PATCH/PUT /children/1.json
   def update
     respond_to do |format|
       if @child.update(child_params)
         format.html { redirect_to @child, notice: 'Kind wurde erfolgreich angepasst.' }
         format.json { render :show, status: :ok, location: @child }
       else
         format.html { render :edit }
         format.json { render json: @child.errors, status: :unprocessable_entity }
       end
     end
   end

   # DELETE /children/1
   # DELETE /children/1.json
   def destroy
     @child.destroy
     respond_to do |format|
       format.html { redirect_to children_url, notice: 'Kind wurde erfolgreich ausgetragen.' }
  format.json { head :no_content }
     end
   end

   private

     # Use callbacks to share common setup or constraints between actions.
     def set_child
       @child = Child.find(params[:id])
     end

     # Never trust parameters from the scary internet, only allow the white list through.
     def child_params
       params.require(:child).permit(:name, :city, :postalcode, :streed, :add_number, :disability, :halal, :koscha, :vegetarian, :vegan, :allday, :gender)
     end

     def set_relation
       @relation = Relation.find(params[:id])
     end

     # Never trust parameters from the scary internet, only allow the white list through.
     def relation_params
       # params.require(:relation).permit(:kiga_id, :child_id, :assignment, :preference, :distance)
     end
 end

和我的控制器测试如下:

 require 'test_helper'

 class ChildrenControllerTest < ActionDispatch::IntegrationTest
   setup do
     @child = children(:one)
   end

    test "should get index" do
      get children_url
      assert_response :success
    end

   test "should get new" do
     get new_child_url
     assert_response :success
   end

     test "should create child" do
       assert_difference('Child.count') do
         post children_url, params: { child: { add_number: @child.add_number, allday: @child.allday, city: @child.city, disability: @child.disability, gender: @child.gender, halal: @child.halal, koscha: @child.koscha, name: @child.name, postalcode: @child.postalcode, streed: @child.streed, vegan: @child.vegan, vegetarian: @child.vegetarian } }
       end

       assert_redirected_to child_url(Child.last)
     end

    test "should show child" do
      get child_url(@child)
      assert_response :success
    end

   test "should get edit" do
     get edit_child_url(@child)
     assert_response :success
   end

    test "should update child" do
      patch child_url(@child), params: { child: { add_number: @child.add_number, allday: @child.allday, city: @child.city, disability: @child.disability, gender: @child.gender, halal: @child.halal, koscha: @child.koscha, name: @child.name, postalcode: @child.postalcode, streed: @child.streed, vegan: @child.vegan, vegetarian: @child.vegetarian } }
      assert_redirected_to child_url(@child)
         end

   test "should destroy child" do
     assert_difference('Child.count', -1) do
       delete child_url(@child)
     end

     assert_redirected_to children_url
   end
 end

我还定义了一些能力:

  class Ability
    include CanCan::Ability

    def initialize(user)

      if user.admin?
        can :manage, :all
        can :view, Child
        can :view, Kiga
        can :read, Kiga
        can :read, Child
        can :read, User

      elsif user.role == 'Eltern'
         can [:update, :destroy], Child do |child|
           child.user_id == user.id
         end
         can :view, Child do |child|
           child.user_id == user.id
         end

         can :read, Child do |child|
           child.user_id == user.id
         end
         can :create, Child

         can :read, Kiga

         can :results_child, Child

         can [:read, :view], Relation do |relation|
           relation.user_id == user.id
           end

     else

   end

   end
 end

我正在使用test_helpercancan用于能力。我认为我的能力变化破坏了我的测试,因为我的index, show,createupdatetest 在以下按摩中产生错误ActionView::Template::Error: undefined method 'admin?' for nil:NilClass

有人可以帮助我并告诉我,我必须如何重新安排我的控制器和测试吗?还是有另一个大错误?非常感谢你!

沙德维尔

user.admin?电话是什么导致错误。

ability.rb该得到由cancancan产生包括注释线作为示例的一部分:

# user ||= User.new # guest user (not logged in)

这是因为当您没有登录用户nil,系统会传入以创建Ability实例。

有了user ||= User.new它意味着您有一个实际的但未保存的用户实例与之交互。将该行作为initialize方法中的第一行Ability可能比让所有用户检查诸如if user && user.admin?.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章