2015-03-31 76 views
0

嘗試通過ajax創建rspec測試以獲取方法。重試302狀態,但期望200. Post方法的測試返回相同的結果。 user_pages_spec.rb嘗試通過ajax創建rspec測試以獲取方法

require 'spec_helper' 

describe "ProgectPage" do 

    subject { page } 
    let(:user) { FactoryGirl.create(:user) } 


    before(:each) do 
    visit signin_path 
     fill_in "Email", with: user.email.upcase 
     fill_in "Password", with: user.password 
     click_button "Sign in" 
    end 

    describe "createProgect" do 
    describe "vith invalid data" do 
     before do 
     click_link "Add TODO list" 
     fill_in "progect_title", with: 'Progect new' 
     fill_in "progect_date_end", with: '' 
     click_button "Save" 
     end 
     it { should have_selector('div.alert.alert-error') } 
    end 
    describe "vith valid data" do 
     before do 
     click_link "Add TODO list" 
     fill_in "progect_title", with: 'Progect new' 
     fill_in "progect_date_end", with: '2014-10-10' 
     end 
     it "should increment the progect count" do 
      expect do 
      click_button "Save" 
     end.to change(user.progects, :count).by(1) 
     end 
    end 
    end 


    it "has a 200 status code" do 
    #p user.inspect 
    xhr :get, '/users/1' 
    # p response.inspect 
    response.code.should == "200" 
    end 

    # it "has a 200 status code" do 
    # xhr :post, '/progects', progect: {title: "First Progect", date_end: "2014-10-11", user_id: 1} 
    # p response.inspect 
    # response.code.should == "200" 
    # end 

end 

耙路由

Prefix Verb URI Pattern    Controller#Action 
    duties_new GET /duties/new(.:format)  duties#new 
    users_new GET /users/new(.:format)  users#new 
     users GET /users(.:format)   users#index 
       POST /users(.:format)   users#create 
    new_user GET /users/new(.:format)  users#new 
    edit_user GET /users/:id/edit(.:format) users#edit 
     user GET /users/:id(.:format)  users#show 
       PATCH /users/:id(.:format)  users#update 
       PUT /users/:id(.:format)  users#update 
       DELETE /users/:id(.:format)  users#destroy 
    sessions POST /sessions(.:format)  sessions#create 
    new_session GET /sessions/new(.:format) sessions#new 
     session DELETE /sessions/:id(.:format) sessions#destroy 
    progects POST /progects(.:format)  progects#create 
     progect DELETE /progects/:id(.:format) progects#destroy 
     duties POST /duties(.:format)   duties#create 
     duty DELETE /duties/:id(.:format)  duties#destroy 
     root GET /      sessions#new 
     signin GET /signin(.:format)   sessions#new 
     signout DELETE /signout(.:format)  sessions#destroy 
       PUT /duties/:id(.:format)  duties#update 
duties_updpos POST /duties/updpos(.:format) duties#updpos 
       PUT /progects/:id(.:format) progects#update 

束EXEC rspec的規格/請求/ user_pages_spec.rb

F.. 

Failures: 

    1) ProgectPage has a 200 status code 
    Failure/Error: response.code.should == "200" 
     expected: "200" 
      got: "302" (using ==) 
    # ./spec/requests/user_pages_spec.rb:45:in `block (2 levels) in <top (required)>' 

Finished in 0.6253 seconds 
3 examples, 1 failure 

Failed examples: 

rspec ./spec/requests/user_pages_spec.rb:41 # ProgectPage has a 200 status code 

UPD

it "has a 200 status code" do 
    xhr :get, :show, id: 1 
    response.code.should == "200" 
end 

有錯誤

F.. 

Failures: 

    1) ProgectPage has a 200 status code 
    Failure/Error: xhr :get, :show, id: 1 
    ArgumentError: 
     bad argument (expected URI object or URI string) 
    # ./spec/requests/user_pages_spec.rb:44:in `block (2 levels) in <top (required)>' 

Finished in 0.6102 seconds 
3 examples, 1 failure 

Failed examples: 

rspec ./spec/requests/user_pages_spec.rb:41 # ProgectPage has a 200 status code 

Randomized with seed 13402 

回答

0

您檢查日誌文件,以確保但我認爲你會發現,你所得到的302響應您的登錄系統,您重定向到註冊頁面。

問題是你在你的規格中混合了2件東西:水豚(你用來登錄的)和導軌集成測試助手(當你打電話給xhr時)。雖然它們看起來很相似,但它們是完全獨立的 - 一個不能看到另一個設置的cookie,所以你的ajax請求被拒絕,因爲沒有登錄。一般情況下,在一個規範中混合這兩個並不好(我甚至不會既可以在同一個文件中使用,也可以使用同一個項目,因爲可能會造成混淆)。

如果你選擇通過下水豚的方式來解決這個問題,那麼不是直接發出ajax請求,而是觸發它點擊一個按鈕(或者任何ui動作應該觸發它)並檢查DOM的變化某種程度上來說。

如果你走下集成測試路線,那麼你需要使用集成測試方法(獲取,發佈等)或者甚至直接通過會話進行登錄請求。你甚至可以決定一個控制器規格更適合你想測試的東西