我的测试有问题。我使用脚手架来创建我的应用程序,但在我的控制器中改变了一些东西。我的第一个控制器看起来如下
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_helper
和cancan
获取能力。我认为我的能力改变破坏了我的测试,因为我的index
,show
,create
和update
测试会产生以下按摩错误
ActionView::Template::Error: undefined method 'admin?' for nil:NilClass
有人可以帮助我并告诉我,我如何重新安排我的控制器和测试?还是有另一个大错误? 非常感谢你!
答案 0 :(得分:0)
user.admin?
来电是造成错误的原因。
cancancan生成的ability.rb
包含注释掉的行作为示例的一部分:
# user ||= User.new # guest user (not logged in)
这是因为当您没有登录用户nil
时,会传入以创建Ability
实例。
使用user ||= User.new
表示您有一个实际但未保存的用户实例进行交互。将该行放在initialize
Ability
if user && user.admin?
方法中的第一行可能比让所有用户检查ST_Difference
更容易。