master
 1require "rails_helper"
 2
 3describe Api::V1::TutorialsController do
 4  render_views
 5  let(:user) { create(:user) }
 6
 7  before :each do
 8    api_login(user)
 9  end
10
11  describe "#index" do
12    let!(:my_tutorial) { create(:tutorial, user: user) }
13    let!(:other_tutorial) { create(:tutorial) }
14
15    it "returns the users tutorials" do
16      get :index, xhr: true
17      expect(assigns(:tutorials)).to match_array([my_tutorial])
18    end
19  end
20
21  describe "#create" do
22    it "creates a new tutorial" do
23      attributes = {
24        url: "https://twitter.com/",
25        image_url: "https://abs.twimg.com/a/img/t1/lemon.jpg",
26        heading: "Twitter",
27        description: "Connect with your friends - and other fascinating people",
28        tags: "cake,cookie",
29      }
30      post :create, params: { tutorial: attributes }, xhr: true
31
32      expect(assigns(:tutorial)).to be_present
33      expect(assigns(:tutorial).url).to eql(attributes[:url])
34      expect(assigns(:tutorial).description).to eql(attributes[:description])
35      expect(assigns(:tutorial).heading).to eql(attributes[:heading])
36      expect(assigns(:tutorial).tags.count).to eql(2)
37      expect(assigns(:tutorial).tags.pluck(:name)).to match_array(["cake", "cookie"])
38    end
39  end
40end