2011-06-11 34 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是你的朋友!