2011-06-11 33 views
1

我正在使用backbone.js和rails。我的控制器接受并返回JSON。我的所有测试都正常工作,直到我添加了设计认证。我不能让黄瓜保持认证。设计,黄瓜和水豚不在JSON请求上进行验证

我已经试过各种上提到:

  • devise wiki
  • here - 标准登录(进入页面,填写表格,单击按钮),设置cookie,并有过滤器之前,一个特殊的应用,做但后者在机架(无法得到这个甚至可以运行)
  • 做上述的Cookie /使用的before_filter this set_cookie method

一切都无济于事。登录返回罚款(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 

这里是一个黄瓜特点:

Scenario: View contacts 
Given I am logged in as "[email protected]" 
And I send and accept JSON 
And the following contacts: 
    |name| 
    |name 1| 
    |name 2| 
    |name 3| 
    |name 4| 
When I send a GET request for "/contacts" 
Then the response should be "200" 
And the JSON response should be an array with 4 "name" elements 
And I should get the following contacts: 
    |name| 
    |name 1| 
    |name 2| 
    |name 3| 
    |name 4| 

的JSON的方法是直接用机架。我怀疑这是我的问题所在,但我不知道如何克服它。

这里是黄瓜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 

回答

1

你的黄瓜看起来像一个RSpec的测试真的......既然你没有实际的渲染,除了从控制器直接输出任何东西,我通常跳过重的黄瓜测试更灵活的RSpec测试。 Cuke留下来进行整合,通常会测试控制器和视图,以推动业务成果的一系列场景。

的关键问题可能是:

Given I am logged in as "[email protected]" 

我不知道做什么。如果它只是显示一个登录表单而不做任何事情,那么你会得到一个200.如果你想解决这个问题,请逐步完成登录步骤,并确保你有一个user_session,当你点击帖子/打电话。

user_signed_in?.should be_true 
current_user.should_not be_nil 
user_session.should_not be_nil 

如果您只想简单地测试单个控制器操作的直接结果,请尝试使用RSpec。

下面是一个控制器,我在RSpec的样品规格:(最小的一个我能找到的W/AUTH)

require "spec_helper" 

describe GuestsController do 
    include Devise::TestHelpers 

    let(:user) { Factory(:user) } 

    context "#create" do 
    it "should create an anonymous user" do 
     User.should_receive(:anonymous!).and_return(user) 

     post :create 

     response.should redirect_to(meals_path) 
    end 

    it "should not create a user" do 
     User.should_not_receive(:anonymous!) 

     sign_in user 
     post :create 

     response.should redirect_to(meals_path) 
    end 
    end 
end 

你甚至可以踩灭了认证,如果你不想sign_in

打电话给我们
before do 
    request.env['warden'] = mock(Warden, authenticate: mock_user, 
             authenticate!: mock_user) 
end 

祝你好运。 debugger是你的朋友!