0

私のテストでカピバラが 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

私はspecとdeviseを使用しています。レンダリングされたフォームは次のとおりです。

<%= 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 と一緒にテストするには、統合テストを行います。(統合 = 一度に複数のもの!)

例えば:

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 に答える