Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rspec能否将数据保存到数据库?_Ruby On Rails_Json_Rspec - Fatal编程技术网

Ruby on rails rspec能否将数据保存到数据库?

Ruby on rails rspec能否将数据保存到数据库?,ruby-on-rails,json,rspec,Ruby On Rails,Json,Rspec,通过HttpParty的json post创建用户会创建用户(如服务器日志所示),但测试在这两种情况下都失败。用户计数未更改或找不到用户。这是我对测试、规范的理解还是rspec处理创建db记录的方式的问题 结果: 1) User signup and signin with native request with valid information should save the user Failure/Error: User.where(email: "bigbadwolf@e

通过HttpParty的json post创建用户会创建用户(如服务器日志所示),但测试在这两种情况下都失败。用户计数未更改或找不到用户。这是我对测试、规范的理解还是rspec处理创建db记录的方式的问题

结果:

  1) User signup and signin with native request with valid information should save the user
     Failure/Error: User.where(email: "bigbadwolf@example.com").should exist
       expected #<ActiveRecord::Relation []> to exist

  1) User signup and signin with native request with valid information should save the user
     Failure/Error: expect {
       result should have been changed by 1, but was changed by 0
日志:


Rspec将数据保存到开发数据库,而不是测试数据库。我需要配置测试模式

rails s -e test
强制rspec使用测试数据库

# spec_helper.rb
ENV["RAILS_ENV"] = 'test'
在测试模式下运行服务器

rails s -e test

你能分享一下问题中失败的例子吗?你不使用工厂或RSpec内置的
post
功能有什么原因吗?您可以执行
post”/login“,:username=>“jdoe”,:password=>“secret”
etc这是否包括自定义标题?我正在使用httparty,以便添加自定义标题。
rails s -e test