2016-12-05 4 views
1
 
RSpec.describe 'Book', :type => :request do 
    describe 'List Books' do 
    let(:book) { FactoryGirl.create :book } 
    before(:each) do 
     visit root_path 
    end 
    context 'when book is created' do 
     let(:book) { FactoryGirl.create :book, title: 'My Book Title' } 
     it 'should list books' do 
     post books_path({book: book})   
     expect(page).to have_content 'My Book Title' 
     end 
    end 
    context 'when no book is created' do 
     it 'should not list books' do 
     expect(page).to have_content 'No books found' 
     end 
    end 
    end 
end 

エラー:params.require(:book).permit(:title、:abstract、:author、:pages、price:image_file_name、:genre、:published_on)Rspec rails routesエラーパラメータがありません

ActionController::ParameterMissing: 
    param is missing or the value is empty: book 

答えて

0

を変更することができ、あなたのコントローラでこの

RSpec.describe 'Book', :type => :request do 
    describe 'List Books' do 
    let(:book) { FactoryGirl.create :book } 
    before(:each) do 
     visit root_path 
    end 
    context 'when book is created' do 
     let(:book) { FactoryGirl.create :book, title: 'My Book Title' } 
     it 'should list books' do 
     post books_path({book: book}) 
     #visit root_path(book) 
     #expect(page).to have_content 'Displaying 1 book' 
     expect(page).to have_content 'No books found' 
     end 
    end 
    context 'when no book is created' do 
     it 'should not list books' do 
     expect(page).to have_content 'No books found' 
     end 
    end 
    end 
end 

を試してみてください

params.require(:book).permit(:title, :abstract, :author, :pages, :price, :image_file_name, :genre, :published_on) 

問題は、これらの値をブックパラメータに渡すことを許可したいということです。ブックが{book => {'title' => 'Cat and the Hat', 'auther' => 'Me'}}のようなハッシュでない限り、許可メソッドは正しく動作しません。したがって、パラメータの構造を変更するか、またはホワイトリストに2つの手順を入れます。

+0

のリストをテストする必要性を満たすん、それはのための未定義のメソッド '許可証」を示しています"424":文字列 – jerrytom

+0

予約の詳細ページにリダイレクトしています – jerrytom

+0

コントローラを追加できますか –

0

このエラーはpost books_path(book)によって発生します。 正しい実装は次のとおりです。私はインスタンス変数を作成するためにレッツを使用しています以下の例では

RSpec.describe 'Book', :type => :request do 
    describe 'List Books' do 
    let(:book) { FactoryGirl.create :book } 
    before(:each) do 
     visit root_path 
    end 
    context 'when book is created' do 
     let!(:book) { FactoryGirl.create :book, title: 'My Book Title' } 
     it 'should list books' do 
     expect(page).to have_content 'Displaying 1 book' 
     expect(page).not_to have_content 'No books found' 
     end 
    end 
    context 'when no book is created' do 
     it 'should not list books' do 
     expect(page).to have_content 'No books found' 
     end 
    end 
    end 
end 
+0

予約にリダイレクトされています詳細ページ書籍をリストにしない – jerrytom

+0

投稿の正しい実装は何ですか – jerrytom

+0

仕様のroot_pathとは何ですか?ポイントは、 'let!(:book)'を呼ぶことによってコントローラを打つことなく本を作ることです。現在の実装では、ポストアクション、ブック作成後のリダイレクト、またはインデックスビューなど、何をテストしているのかは分かりません。 –

0

。 letを使用すると、変数lazyはテストで初めて使用されたときにのみロードされ、特定のテストが終了するまでキャッシュされます。 は、したがって、私は本のインスタンスは、一例のために作成されますと葯例えば作成した何本が

 
RSpec.describe 'Book', :type => :request do 
    describe 'List Books' do 
    before(:each) do 
     visit books_path 
    end 
    context 'when book is created' do 
     let!(:book) { FactoryGirl.create :book, title: 'My Book Title' } 
     it 'should list books' do 
     expect(page).to have_content 'Displaying all 2 book' 
     expect(page).to have_content 'My Book Title' 
     end 
    end 
    context 'when no book is created' do 
     it 'should not list books' do 
     expect(page).to have_content 'No books found' 
     end 
    end 
    end 
end 

をテストしていないために、このテストは完全な書籍

関連する問題