簡體   English   中英

RSpec:在控制器中測試創建操作方法

[英]RSpec: Test a create action method in controller

我已經為控制器中的創建操作方法編寫了 RSpec 測試。 現在我變成了以下錯誤:錯誤屏幕

我的測試:

describe '#create' do
before(:each) {
  @address = {
    attributes: {
      'street': "bla",
      'street-number': 2,
      'zip': "12345",
      'city': "blabla",
      'country': ''
    },
    type: "addresses"
  }
}


it 'test the create route' do
  post 'create', { params: @address }
end
end

我的控制器方法:

public def create
  action(Address::Create)
    .otherwise('address_create_error', 401)
    .then_render(Address::Representer::Out::Default)
end

還有我的工廠:

FactoryGirl.define do
  factory :address do
    street 'Musterstraße'
    street_number '1'
    zip '12345'
    city 'Musterstadt'
    country ''
  end
end

我不知道為什么會出現這個錯誤。 有人可以幫我嗎?

嘗試使用“=>”語法

 @address = {
    'attributes' => {
      'street' => "bla",
      'street-number' => 2,
      'zip' => "12345",
      'city' => "blabla",
      'country': ''
    },
    'type' => "addresses"
  }

我今天已經解決了這個問題。 為了解決這個問題,我不得不調整兩件事。

  1. JSON 必須完全作為字符串傳輸
  2. 它必須在請求正文中發送,而不是作為參數發送。 那是因為我們的特殊環境。

還是要謝謝你的幫助。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM