2016-12-05 72 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) 

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

的問題是,你說我想允許這些值傳遞入冊參數。除非本書就像{book => {'title' => 'Cat and the Hat', 'auther' => 'Me'}}哈希許可證方法將無法在其正常工作。所以,要麼改變分兩步

+0

它顯示未定義的方法'許可證」的「424」:字符串 – jerrytom

+0

它將它重定向到書籍詳細信息頁面而不是列出書籍 – jerrytom

+0

您可以添加您的控制器 –

0

此錯誤是由post books_path(book)引起你則params的結構,或白名單他們。 正確的實現是:

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

在下面的例子中,我使用讓我們來創建一個實例變量。使用讓可變延遲加載只有當它被用來在第一時間測試並拿到緩存,直到特定測試完成。 因此我將書實例的一個示例中創建並沒有花葯例如創建書,以測試

 
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 

做這個測試完全充滿需要測試本書的名單

相關問題