Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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 Desive、cucumber和capybara未根据JSON请求进行身份验证_Ruby On Rails_Json_Devise_Cucumber - Fatal编程技术网

Ruby on rails Desive、cucumber和capybara未根据JSON请求进行身份验证

Ruby on rails Desive、cucumber和capybara未根据JSON请求进行身份验证,ruby-on-rails,json,devise,cucumber,Ruby On Rails,Json,Devise,Cucumber,我在rails中使用backbone.js。我的控制器接受并返回JSON。在我加入Desive认证之前,我所有的测试都进行得很好。我不能让cucumber保持身份 我已经尝试了上面提到的一切: -标准登录(进入页面,填写表单,点击按钮),设置cookie,在筛选之前有一个特殊的应用程序,在机架中执行后一个操作(甚至无法运行) 使用 都没有用。登录返回很好(200),但当我发送请求时,会得到401响应 这是我的控制器代码: class ContactsController < Appl

我在rails中使用backbone.js。我的控制器接受并返回JSON。在我加入Desive认证之前,我所有的测试都进行得很好。我不能让cucumber保持身份

我已经尝试了上面提到的一切:

  • -标准登录(进入页面,填写表单,点击按钮),设置cookie,在筛选之前有一个特殊的应用程序,在机架中执行后一个操作(甚至无法运行)
  • 使用
都没有用。登录返回很好(200),但当我发送请求时,会得到401响应

这是我的控制器代码:

class ContactsController < ApplicationController
  before_filter :authenticate_user!

  ### backbone
  def index
    render :json => current_user.person.contacts
  end

  def show
    render :json =>  current_user.person.contacts.find(params[:id])
  end

  def create
    contact = current_user.person.contacts.create! params
    render :json => contact
  end

  def update
    contact =  current_user.person.contacts.find(params[:id])
    contact.update_attributes! params
    render :json => contact
  end

 def destroy
    contact =  current_user.person.contacts.find(params[:id]) 
    contact.destroy
    render :json => contact  #TODO: change to render nothing
  end
JSON方法直接使用Rack。我怀疑这就是我的问题所在,但我不知道如何克服它

以下是cuke JSON步骤:

World(Rack::Test::Methods)

Given /^I send and accept JSON$/ do
  header 'Accept', 'application/json'
  header 'Content-Type', 'application/json'
end

When /^I send a GET request for "([^\"]*)"$/ do |path|
  get path
end

When /^I send a POST request to "([^\"]*)" with the following:$/ do |path, table|
  params = table.hashes.flatten[0].to_json
  post path, params
end

When /^I send a PUT request to "([^\"]*)" with the following:$/ do |path, table|
  params = table.hashes.flatten[0].to_json
  put path, params
end

When /^I send a DELETE request to "([^\"]*)"$/ do |path|
  delete path
end

Then /^the response should be "([^\"]*)"$/ do |status|
  last_response.status.should == status.to_i
end

Then /^the JSON response should have 1 "([^\"]*)" element$/ do |name|
  page = JSON.parse(last_response.body)
  page[name].should be
end

Then /^the JSON response should be an array with (\d+) "([^\"]*)" elements$/ do |number_of_children, name|
  page = JSON.parse(last_response.body)
  page.map { |d| d[name] }.length.should == number_of_children.to_i
end

你的Cuke看起来像RSpec测试真的。。。因为除了控制器的直接输出之外,您实际上没有渲染任何东西,所以我通常跳过较重的Cuke测试,以获得更灵活的RSpec测试。Cuke被留下来进行集成,通常在一系列驱动业务结果的场景中测试控制器和视图

关键问题可能在于:

Given I am logged in as "testy@testing.com"
我不知道那有什么用。如果它只是显示一个登录表单,而不做任何处理,您将得到一个200。如果您想解决这个问题,请逐步完成登录步骤,并确保在您点击post/get调用时拥有用户会话

user_signed_in?.should be_true
current_user.should_not be_nil
user_session.should_not be_nil
如果您只想简单地测试单个控制器操作的直接结果,请尝试使用RSpec

以下是我在RSpec中拥有的控制器的示例规范:(我能找到的最小的一个w/auth)

如果不想呼叫登录,甚至可以取消身份验证

before do
  request.env['warden'] = mock(Warden, authenticate: mock_user,
                                       authenticate!: mock_user)
end

祝你好运<代码>调试器是你的朋友

你的Cuke看起来像RSpec测试真的。。。因为除了控制器的直接输出之外,您实际上没有渲染任何东西,所以我通常跳过较重的Cuke测试,以获得更灵活的RSpec测试。Cuke被留下来进行集成,通常在一系列驱动业务结果的场景中测试控制器和视图

关键问题可能在于:

Given I am logged in as "testy@testing.com"
我不知道那有什么用。如果它只是显示一个登录表单,而不做任何处理,您将得到一个200。如果您想解决这个问题,请逐步完成登录步骤,并确保在您点击post/get调用时拥有用户会话

user_signed_in?.should be_true
current_user.should_not be_nil
user_session.should_not be_nil
如果您只想简单地测试单个控制器操作的直接结果,请尝试使用RSpec

以下是我在RSpec中拥有的控制器的示例规范:(我能找到的最小的一个w/auth)

如果不想呼叫登录,甚至可以取消身份验证

before do
  request.env['warden'] = mock(Warden, authenticate: mock_user,
                                       authenticate!: mock_user)
end
祝你好运<代码>调试器是你的朋友