RSpec请求示例失败

8维

我正在尝试做一个非常简单的请求规范,以测试我的应用程序中的API方法。现在我收到302消息,而本次测试应该得到200。

规格:

require 'spec_helper'

describe UsersController do
  describe "#create" do
    it "creates a new user " do
      post '/users', user: FactoryGirl.attributes_for(:user)
      expect(response.status).to eq 200
    end
  end
end 

该工厂:

FactoryGirl.define do
  factory :user do
    sequence(:name)  { |n| "Person #{n}" }
    sequence(:username) { |n| "Person#{n}" } 
    sequence(:email) { |n| "person_#{n}@example.com"}
    password "foobar"
    password_confirmation "foobar"
  end
end

控制器方法:

def create
  @user = User.new(user_params)

  respond_to do |format|
    if @user.save
      format.html { redirect_to @user, notice: 'User was successfully created.' }
      format.json { render :show, status: :created, location: @user }
    else
      format.html { render :new }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

我想念什么?

8维

为了解决这个问题,我需要在ACCEPT请求中添加标题,如下所示:

post "/users", { user: @user } , { accept: 'application/json' }

这样做解决了服务器未将请求解释为期望返回json的问题。

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用rspec仅打印失败示例的数量

来自分类Dev

Rspec:如何访问失败示例的当前种子编号

来自分类Dev

RSpec.configure无法正常工作,并且由于rspec-mocks#setup而导致示例失败

来自分类Dev

身份验证请求失败贝宝Android SDK示例

来自分类Dev

是否可以将RSpec配置为使任何不包含任何期望的示例失败?

来自分类Dev

如何为应该在 Pundit 授权机制之后失败的请求编写请求 RSpec?

来自分类Dev

获取Rspec示例标签

来自分类Dev

在rspec / capybara / poltergeist / phantomjs规范中,“ HTTP请求失败”是什么意思?

来自分类Dev

Rails RSpec请求规范失败,因为添加了意外的%2F来重定向响应

来自分类Dev

在rspec / capybara / poltergeist / phantomjs规范中,“ HTTP请求失败”是什么意思?

来自分类Dev

QtSerialPort示例失败

来自分类Dev

QtSerialPort示例失败

来自分类Dev

建筑球衣示例失败

来自分类Dev

RSpec:验证程序的共享示例?

来自分类Dev

跳过RSpec中的多个示例?

来自分类Dev

Hystrix请求缓存的示例

来自分类Dev

请求代码示例的说明

来自分类Dev

Grok RSpec测试失败

来自分类Dev

Rspec,失败测试日期

来自分类Dev

用户规范的 rspec 失败

来自分类Dev

OpenCV 3.0.0 FaceDetect示例失败

来自分类Dev

敏捷中央示例应用失败

来自分类Dev

构建Spock教程示例失败

来自分类Dev

敏捷中央示例应用失败

来自分类Dev

PDFBox Android 失败示例代码

来自分类Dev

请求流星+自动套用示例

来自分类Dev

XACML PolicySet和请求示例

来自分类Dev

Alamofire + Combine + MVVM请求示例

来自分类Dev

请求php foreach stdclass示例