2012-09-15 37 views
4

我從Webrat遷移到水豚,現在我收到了很多錯誤。例如,在webrat我可以使用,在集成測試:Ruby on Rails 3 + Rspec + Capybara:檢查回覆狀態

response.should be_success 

但水豚表明:

Failure/Error: response.should be_success 
    NoMethodError: 
     undefined method `success?' for nil:NilClass 

有沒有提供這種功能的方法?

UPD:我的規格:

require 'spec_helper' 

describe "Admins" do 
    before(:each) do 
    @admin = FactoryGirl.create(:admin) 

    visit '/' 
    click_link "Login" 
    fill_in "Email", :with => @admin.email 
    fill_in "Password", :with => 'qwerty' 
    click_button "Sign in" 
    end 

    describe "Admin panel" do 
    it "should have correct links" do 
     click_link "User" 
     response.should be_success 
    end 
    end 
end 

回答

15

你混合控制器和要求的規格。

在控制器規範中,您檢查response,在請求規範中,您檢查page內容,因爲您只能訪問html。

13

我找不到像success?方法快捷鍵的跡象,但如果你的驅動程序支持它,你可以做這樣的事情:

visit(some_path) 
page.status_code.should == 200 

將其加入而回,但不似乎是有據可查的。此外,一些司機根本不會將信息提供給水豚。硒被特別提及 - 如果你在使用Selenium驅動程序的時候嘗試這個,它會崩潰。

1

這可能會幫助您:

expect(page).to have_http_status(200) 
1

請使用asset_exists的?功能在接下來的要點

def asset_exists?(src) 
    js_script = <<JSS 
    xhr = new XMLHttpRequest(); 
    xhr.open('GET', '#{src}', true); 
    xhr.send(); 
    JSS 

    page.execute_script(js_script) 
    status = page.evaluate_script('xhr.status') # get js variable value 
    status == 200 || status == 302 
end 

https://gist.github.com/yovasx2/1c767114f2e003474a546c89ab4f90db更多細節