簡體   English   中英

Rails 4 Rspec Capybara / Selenium:內部服務器錯誤

[英]Rails 4 Rspec Capybara/Selenium : Internal server error

每當嘗試去這條路

 localhost:8080/todos

它應該轉到待辦事項/索引頁面。 實際上,這是單頁應用程序。 因此路徑為/ todos。 但是每次運行測試時都會收到內部服務器錯誤。 有人可以幫幫我!

我的日志文件:

    Displaying todos should display the list of todos
 Failure/Error: page.should have_selector('h1', :text => 'Todos')
 Capybara::ExpectationNotMet:
   expected to find css "h1" with text "Todos" but there were no matches
 # ./spec/features/todos/index_spec.rb:26:in `block (2 levels) in <top (required)>'

我的控制器:todos_controller.rb

     skip_before_action :authenticate_user!

    def index
     @todos_completed_today = current_user.todos.where(is_completed: true).
     where("updated_at >= ?", (Time.zone.now - 24.hours)).order('todos.position ASC')
     @todos = current_user.todos.where(is_completed: false).order('todos.position ASC')
  end

  def create
   @todo = Todo.find(params[:id])
   @todo = current_user.todos.new(todo_params)
   @todo.save
    respond_to do |format|
    format.html { redirect_to todos_path }
    format.js
  end
 end

def edit
 @todo = current_user.todos.find_by_id(params[:id])
end

def update
 @todo = current_user.todos.find_by_id(params[:id])
 @todo.update_attributes(todo_params)
end

def destroy
 @todo = current_user.todos.find_by_id(params[:id])
 @todo.destroy
end

測試規格:todos / index_spec.rb

 it "should display the list of todos", :js => true do
visit "/todos"

expect(Todo.count).to eq(0)
page.should have_selector('h1', :text => 'Todos')



fill_in "title", with: "MyString"
click_button "Create"

expect(page).to have_content("Todo has been created!")
expect(todo.todo_title).to eq(title)

todo.reload!
expect(Todo.count).to eq(1)
expect(todo.title).eq("Mystring")

結束

Capybara::ExpectationNotMet: expected to find css "h1" with text "Todos" but there were no matches

您尚未提供視圖,但我將檢查是否有一個帶有選擇器h1的元素,其內容為“ Todos”

根據此測試,您已設置: page.should have_selector('h1', :text => 'Todos')

或者,如果您有h1元素,則可以將測試語法更改為:

page.should have_tag("h1", :text => "Todos")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM