master
1require 'rails_helper'
2
3describe SearchController do
4 describe "GET 'index'" do
5 context "when no search query is given" do
6 before { get :index }
7
8 it "should redirect you to the home page" do
9 expect(response).to redirect_to(root_url)
10 end
11 end
12
13 context "when a valid search query is given" do
14 let!(:user) { create(:user, :name => 'cake') }
15 let!(:bob) { create(:user, :name => 'bob') }
16 let!(:cake) { create(:creation, :name => 'cake') }
17 let!(:donut) { create(:creation, :name => 'donut') }
18 let!(:tutorial) { create(:tutorial, :description => 'cake') }
19 let!(:other_tutorial) { create(:tutorial, :description => 'donut') }
20
21 before { get :index, params: { q: 'cake' } }
22
23 it "returns a successful response" do
24 expect(response).to be_success
25 end
26
27 it "returns all creations that have a matching name" do
28 expect(assigns(:creations)).to include(cake)
29 end
30
31 it "does not include cakes that do not match" do
32 expect(assigns(:creations)).to_not include(donut)
33 end
34
35 it "returns all makers that match" do
36 expect(assigns(:members)).to include(user)
37 end
38
39 it "does not include makers with names that do not match" do
40 expect(assigns(:members)).to_not include(bob)
41 end
42
43 it "returns all tutorials that match" do
44 expect(assigns(:tutorials)).to include(tutorial)
45 end
46
47 it "does not return tutorials that do not match" do
48 expect(assigns(:tutorials)).to_not include(other_tutorial)
49 end
50 end
51 end
52end