简体   繁体   English

RSpec:在控制器中测试创建操作方法

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

I have write a RSpec Test for a create action method in a controller.我已经为控制器中的创建操作方法编写了 RSpec 测试。 Now i became the following error: error Screen现在我变成了以下错误:错误屏幕

My Test:我的测试:

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

My Controller Method:我的控制器方法:

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

And my Factory:还有我的工厂:

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

I have no Idea why this error comes.我不知道为什么会出现这个错误。 Could anybody help me?有人可以帮我吗?

Try using the "=>" syntax尝试使用“=>”语法

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

I have resolved the problem today.我今天已经解决了这个问题。 To solve it, I had to adjust two things.为了解决这个问题,我不得不调整两件事。

  1. the JSON must be transferred completely as a string JSON 必须完全作为字符串传输
  2. it must be sent in the body of the request, not as a param.它必须在请求正文中发送,而不是作为参数发送。 That's because of our special environment.那是因为我们的特殊环境。

Thanks for your help anyway.还是要谢谢你的帮助。 :) :)

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

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