【发布时间】: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