gpt4 book ai didi

ruby-on-rails - Ruby on Rails - 测试时的 TDD 测试错误

转载 作者:行者123 更新时间:2023-11-28 21:28:10 25 4
gpt4 key购买 nike

我要回去努力学习 TDD 测试了。我正在编写一个教程,该教程希望我创建将与主题相关联的 SponsoredPosts。

我是 TDD 测试的新手,所以遇到了一些小问题。

我还没有创建一个包含两个词“赞助”“帖子”的 Controller 或模型,所以我不确定处理它们时的协议(protocol),所以我将为你提供我认为相关的一切然后希望你能告诉我哪里出错了。我相信我只是混淆了模型和 Controller 的名称,因为正如您在我的模式文件中看到的那样,“sponsored_posts”表有一个“topic_id”属性。我承认,就知道如何处理模型或 Controller 而言,我不是最擅长命名模型或 Controller 的,因此您还可以提供任何解释以进一步帮助我理解我在命名它们时出错的地方,这将非常有帮助.

这是我的 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 Controller :

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 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.

这是我运行模型规范时的输出:

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)>'

最佳答案

这里有什么我可以给你的建议:

  • 您的 Controller 中有错别字,应该是 SponsoredPost 而不是 Sponsored_post
  • 在您的 Controller 测试中, undefined variable sponsored_posts,可能是:

    SponsoredPost.create!(标题:RandomData.random_sentence,正文:RandomData.random_paragraph,价格:6,主题:my_topic)

  • 在你的模型测试中,它似乎是同样的问题。

关于ruby-on-rails - Ruby on Rails - 测试时的 TDD 测试错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33506037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com