mardi 3 novembre 2015

Ruby on Rails - TDD Testing errors while testing

I am going back and trying hard to learn TDD testing. I am working on a tutorial that wants me to create SponsoredPosts which will be associated with Topics.

I am rather new to TDD testing so I've hit a bit of a hiccup.

I haven't yet had to create a controller or model that consist of two words "Sponsored" "Posts" so I am unsure of the protocol when handling them, so I am going to provide you with everything I think is relevant then hopefully you can tell me where I went wrong. I believe I've just got the names of the model and controllers all mixed up, because as you can see in my schema file there is an attribute of "topic_id" for the table of "sponsored_posts". I'll confess I'm not the best at naming models or controllers in regards to knowing how to handle them so any explanation you could also provide to further help me understand where I went wrong in naming them would be extremely helpful.

Here is my Sponsored_Posts_Controller_Spec:

require 'rails_helper'
include RandomData

RSpec.describe SponsoredPostsController, type: :controller do

  let (:my_topic) { Topic.create!(name: RandomData.random_sentence, description: RandomData.random_paragraph) }
  let (:my_sponsored_post) { my_topic.sponsored_posts.create!(title: RandomData.random_sentence, body: RandomData.random_paragraph, price: 6) }

  describe "GET show" do

    it "returns http success" do
      get :show, topic_id: my_topic.id, id: my_sponsored_post.id
      expect(response).to have_http_status(:success)
    end

    it "renders the #show view" do
      get :show, topic_id: my_topic.id, id: my_sponsored_post.id
      expect(response).to render_template :show
    end

    it "assigns my_sponsored_post to @sponsored_post" do
      get :show, topic_id: my_topic.id, id: my_sponsored_post.id
      expect(assigns(:sponsored_posts)).to eq(my_sponsored_post)
    end
  end


end

Here is my model spec for Sponsored_Post:

require 'rails_helper'
include RandomData

RSpec.describe SponsoredPost, type: :model do
  let(:topic) { Topic.create!(name: RandomData.random_sentence, description: RandomData.random_paragraph) }

  let(:sponsored_post) { topic.sponsored_posts.create!(title: RandomData.random_sentence, body: RandomData.random_paragraph, price: 6) }

  it { should belong_to(:topic) }

  describe "attributes" do

    it "should respond to title" do
      expect(sponsored_posts).to respond_to(:title)
    end

    it "should respond to body" do
      expect(sponsored_posts).to respond_to(:body)
    end

    it "should respond to price" do
      expect(sponsored_posts).to respond_to(:price)
    end

  end
end

Here is my SponsoredPosts Controller:

class SponsoredPostsController < ApplicationController

  def show
    @sponsored_post = Sponsored_post.find(params[:id])
  end

  def new
  end

  def edit
  end

end

Here is my SponsoredPost model:

class SponsoredPost < ActiveRecord::Base
  belongs_to :topic
end

Here is my schema file:

ActiveRecord::Schema.define(version: 20151102190350) do

  create_table "advertisements", force: :cascade do |t|
    t.string   "title"
    t.text     "copy"
    t.integer  "price"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "comments", force: :cascade do |t|
    t.text     "body"
    t.integer  "post_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "comments", ["post_id"], name: "index_comments_on_post_id"

  create_table "posts", force: :cascade do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "topic_id"
  end

  add_index "posts", ["topic_id"], name: "index_posts_on_topic_id"

  create_table "questions", force: :cascade do |t|
    t.string   "title"
    t.text     "body"
    t.boolean  "resolved"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "sponsored_posts", force: :cascade do |t|
    t.string   "title"
    t.text     "body"
    t.integer  "price"
    t.integer  "topic_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "sponsored_posts", ["topic_id"], name: "index_sponsored_posts_on_topic_id"

  create_table "topics", force: :cascade do |t|
    t.string   "name"
    t.boolean  "public",      default: true
    t.text     "description"
    t.datetime "created_at",                 null: false
    t.datetime "updated_at",                 null: false
  end

end

Here is the output when I run the spec for the SponsoredPost Controller:

1) SponsoredPostsController GET show returns http success
     Failure/Error: let (:my_sponsored_post) { my_topic.sponsored_posts.create!(title: RandomData.random_sentence, body: RandomData.random_paragraph, price: 6) }
 ActiveRecord::UnknownAttributeError:
   unknown attribute 'topic_id' for SponsoredPost.

2) SponsoredPostsController GET show renders the #show view
     Failure/Error: let (:my_sponsored_post) { my_topic.sponsored_posts.create!(title: RandomData.random_sentence, body: RandomData.random_paragraph, price: 6) }
 ActiveRecord::UnknownAttributeError:
   unknown attribute 'topic_id' for SponsoredPost.

3) SponsoredPostsController GET show assigns my_sponsored_post to @sponsored_post
     Failure/Error: let (:my_sponsored_post) { my_topic.sponsored_posts.create!(title: RandomData.random_sentence, body: RandomData.random_paragraph, price: 6) }
 ActiveRecord::UnknownAttributeError:
   unknown attribute 'topic_id' for SponsoredPost.

Here is the output when I run the spec for the model:

1) SponsoredPost should belong to topic
 Failure/Error: it { should belong_to(:topic) }
   Expected SponsoredPost to have a belongs_to association called topic (SponsoredPost does not have a topic_id foreign key.)
 # ./spec/models/sponsored_post_spec.rb:9:in `block (2 levels) in <top (required)>'

2) SponsoredPost attributes should respond to title
 Failure/Error: expect(sponsored_posts).to respond_to(:title)
 NameError:
   undefined local variable or method `sponsored_posts' for #<RSpec::ExampleGroups::SponsoredPost::Attributes:0x007f992170a460>
 # ./spec/models/sponsored_post_spec.rb:14:in `block (3 levels) in <top (required)>'

3) SponsoredPost attributes should respond to body
 Failure/Error: expect(sponsored_posts).to respond_to(:body)
 NameError:
   undefined local variable or method `sponsored_posts' for #<RSpec::ExampleGroups::SponsoredPost::Attributes:0x007f9921701388>
 # ./spec/models/sponsored_post_spec.rb:19:in `block (3 levels) in <top (required)>'

4) SponsoredPost attributes should respond to price
 Failure/Error: expect(sponsored_posts).to respond_to(:price)
 NameError:
   undefined local variable or method `sponsored_posts' for #<RSpec::ExampleGroups::SponsoredPost::Attributes:0x007f99216f9ac0>
 # ./spec/models/sponsored_post_spec.rb:23:in `block (3 levels) in <top (required)>'

Aucun commentaire:

Enregistrer un commentaire