我要回去并努力学习TDD测试。我正在制作一个教程,希望我创建与主题相关联的SponsoredPosts。
我对TDD测试比较陌生,所以我遇到了一些打嗝。
我还没有必要创建一个由两个单词“Sponsored”“Posts”组成的控制器或模型,因此在处理它时我不确定协议,因此我将向您提供我认为相关的所有内容希望你能告诉我哪里出错了。我相信我刚刚将模型和控制器的名称都混淆了,因为正如您在我的模式文件中看到的那样,“spons_posts”表中存在“topic_id”属性。我承认我不是最好的命名模型或控制器,知道如何处理它们,所以你还可以提供任何解释,以进一步帮助我理解我在命名它们时出错的地方非常有帮助< /强>
这是我的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
这是我对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
这是我的SponsoredPosts控制器:
class SponsoredPostsController < ApplicationController
def show
@sponsored_post = Sponsored_post.find(params[:id])
end
def new
end
def edit
end
end
这是我的SponsoredPost模型:
class SponsoredPost < ActiveRecord::Base
belongs_to :topic
end
这是我的架构文件:
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
以下是我为SponsoredPost控制器运行规范时的输出:
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.
以下是运行模型规范时的输出:
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)>'
答案 0 :(得分:1)
我可以为您推荐一下:
SponsoredPost
而不是Sponsored_post
在您的控制器测试中,未定义变量sponsored_posts
,可能是:
SponsoredPost.create!(标题:RandomData.random_sentence,正文:RandomData.random_paragraph,价格:6,主题:my_topic)
在您的模型测试中它似乎是同样的问题。