简体   繁体   English

控制器RSpec:使用自定义ID测试PATCH更新

[英]Controller RSpec: test PATCH update using custom ID

I'm having a challenge write a RSpec controller test for a PATCH update, because the routing and edit uses a secure edit_id that my model generates, instead of the standard 1,2,3,4,5 (sequenced id) that Rails auto-generates. 我正在挑战为PATCH更新编写RSpec控制器测试,因为路由和编辑使用我的模型生成的安全edit_id ,而不是Rails自动的标准1,2,3,4,5(顺序id) -generates。 Basically, I'm not sure how to get my tests to lookup the request to be edited using this edit_id. 基本上,我不知道如何让我的测试使用此edit_id查找要编辑的请求。

My test currently: 我目前的测试:

describe "PATCH edit/update" do

    before :each do
        @testrequest = FactoryGirl.build(:request, name: "James Dong")
    end

    it "located the requested @testrequest" do
        patch :update, id: @testrequest.edit_id, request: FactoryGirl.attributes_for(:request)
        assigns(:request).should eq(@testrequest)
    end

    describe "using valid data" do

        it "updates the request" do
            patch :update, @testrequest.name = "Larry Johnson" 
            @testrequest.reload
            @testrequest.name.should eq("Larry Johnson")
        end
    end

FactoryGirl helper (I've tried both explicitly adding edit_id and not [ie, relying on the model to create the edit_id itself], neither makes a difference) code: FactoryGirl助手(我已经尝试过显式添加edit_id而不是[即,依靠模型来创建edit_id本身],两者都没有区别)代码:

FactoryGirl.define do
    factory :request do |f|
        f.name { Faker::Name.name }
        f.email { Faker::Internet.email }
        f.item { "random item" }
        f.detail { "random text" }
        f.edit_id { "random" }
    end
end

Controller : 控制器

def update
    @request = Request.find_by_edit_id(params[:edit_id])
    if @request.update_attributes(request_params) 
    flash[:success] = "Your request has been updated! We'll respond within one business day."
    redirect_to edit_request_path(@request.edit_id)
    else
    render 'edit'
    end
end

Routing : 路由

get 'edit/:edit_id', to: 'requests#edit', as: 'edit_request'
patch 'requests/:edit_id', to: 'requests#update', as: 'request'

Ok someone helped me figure this out, and I feel very silly. 好的,有人帮我搞清楚了,我觉得很傻。 The "id" that you pass to the patch method can be any id, so instead of trying to set id: edit_it, I should use edit_it in the first place. 你传递给补丁方法的“id”可以是任何id,所以我不应该尝试设置id:edit_it,而应该首先使用edit_it。 Ie, the code that works: 即,有效的代码:

before :each do
    @testrequest = FactoryGirl.build(:request, name: "James Dong")
end

it "located the requested @testrequest" do
    patch :update, edit_id: @testrequest.edit_id, request: FactoryGirl.attributes_for(:request)
    assigns(:request).should eq(@testrequest)
end

describe "using valid data" do

    it "updates the request" do
        patch :update, edit_id: @testrequest.edit_id, request: FactoryGirl.attributes_for(:request, name: "Larry Johnson")
        @testrequest.reload
        @testrequest.name.should eq("Larry Johnson")
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM