【问题标题】:Rails 4 Rspec Capybara/Selenium : Internal server errorRails 4 Rspec Capybara/Selenium:内部服务器错误
【发布时间】:2014-12-08 10:00:39
【问题描述】:

每当我想走这条路时

 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")

结束

【问题讨论】:

    标签: ruby-on-rails ruby selenium rspec rspec-rails


    【解决方案1】:

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

    您尚未提供您的视图,但我会检查您是否有一个带有选择器 h1 的元素,其内容为“Todos”

    根据此测试,您已设置: page.should have_selector('h1', :text =&gt; 'Todos')

    或者,如果您有一个 h1 元素,您可以将您的测试语法更改为:

    page.should have_tag("h1", :text =&gt; "Todos")

    【讨论】:

    • 是的,页面上有一个 h1 元素,其内容为“Todos”。
    • Selector 表示类,请参考我更新的答案以及更改测试的解决方案,假设您要测试 h1 元素
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多