0

Capybara 在我的测试中给了我 NoElementFound 错误我的测试

Describe "agenda pages" do
    subject { page }

    login_user

    describe "Agenda Creation" do
        before { visit users_path }

        describe "with invalid information" do
            it "should not create an agenda" do
                expect { click_button "Post" }.should_not change(Agenda, :count)
            end
            describe "error message" do
                before  { click_button "Post" }
                it { should have_content('error') }
            end
        end

        describe "with valid information" do
            before  { fill_in 'agenda_subject', with: "Lorem Ipsum" }
            it "should create a an agenda" do
                expect { click_button "Post" }.should change(Agenda, :count).by(1)
            end
        end

    end

结尾

我怀疑水豚因为我访问 users_path 而没有找到该元素,它只是没有看到该页面。请注意,行为在生产力方面符合预期并且正在工作)如果我将其更改为 root_path,我会得到同样的错误。我的 spec_helper 文件启用了 url_helpers 并且表单正确且值正确。至少我确定, 表格在帖子的末尾我的路线

   root        /                              users#index
                    root        /                              home#index
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                   users GET    /users(.:format)               users#index
                 agendas POST   /agendas(.:format)             agendas#create
                  agenda DELETE /agendas/:id(.:format)         agendas#destroy

路线文件

Engage::Application.routes.draw do
  authenticated :user do
    root to: 'users#index'
  end
  root :to => "home#index"
  devise_for :users
  resources :users, only: [:index]
  resources :agendas, only: [:create, :destroy]
end

我正在使用规范和设计。这是呈现的形式:

<%= form_for(@agenda) do |f| %>
  <div class="field">
    <%= f.text_area :subject, placeholder: "Compose new agenda..." %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
4

1 回答 1

0

所以,对于控制器测试,你真的不应该使用 capybara。Capybara 是一个用于驱动浏览器的集成测试库。要直接测试控制器,您将设置一个请求并发布/获取/放置/等到有问题的操作,并验证您从操作中得到了您期望的结果。通常这将验证记录是否已创建或检查实例变量的内容。

您的控制器不知道路由。单元测试的重点是测试它与这些其他组件隔离。要与 Capybara 一起测试它,请进行集成测试。(集成 = 一次不止一件事!)

例如:

expect { post :create, agenda: FactoryGirl.attributes_for(:agenda)}.to change(Agenda, :count).by(1)

agenda = FactoryGirl.create(:agenda)
get :show, id: agenda
assigns(:agenda).should eq agenda
于 2012-05-16T20:08:15.637 回答