无法在 Capybara/Cucumber 中创建评论
Can't create a comment in Capybara/Cucumber
我有以下特点:
Scenario: when delete a post
Given I created a post with the title "Test", author "Testauthor" and body "Testbody"
When I browse "/posts" and click on "Destroy"
Then I should see the text "Comment was successfully destroyed."
有问题的代码是这样给定的实现:
Given(/^I created a post with a comment with title "([^"]*)", author "([^"]*)" and body "([^"]*)"$/) do |title, author, body|
post = Post.create! title: 'Test', author: 'Test', body: 'Test', published_at: DateTime.now
Comment.create! title: title, author: author, body: body, published_at: DateTime.now, post_id: post.id
end
由于某种原因 expect(Comment.count).to eq(1)
是假的,没有评论。我做错了什么?
已添加存储库: https://github.com/cwansart/SQ-Hausarbeit/tree/cucumber/features 这是 posts_show.feature 和 posts_show_steps.rb。
我克隆了你的 repo 并看了一下。
Scenario: when delete a post
Given I created a post with the title "Test", author "Testauthor" and body "Testbody"
When I browse "/posts" and click on "Destroy"
Then I should see the text "Comment was successfully destroyed."
posts_show_steps.rb:
Given(/^I created a post with a comment with title "([^"]*)", author "([^"]*)" and body "([^"]*)"$/) do |title, author, body|
post = Post.create! title: 'Test', author: 'Test', body: 'Test', published_at: DateTime.now
Comment.create! title: title, author: author, body: body, published_at: DateTime.now, post_id: post.id
print "#########################"
print Comment.all.pretty_print_inspect
## this shows the comment. It **does** exist...
end
When(/^I browse "([^"]*)" and click on "([^"]*)"$/) do |url, click_text|
print "#########################"
print Comment.all.pretty_print_inspect
## here it doesn't exist any longer...
visit url
end
在这里,你创建了一个post 没有评论,这是第一个问题,即没有要删除的评论!这可以通过将 Given
更改为:
来解决
Given I created a post with a comment with title "CommTitle", author "CommAuthor" and body "CommBody"
(也可以重用该步骤定义,但如果此测试只是创建一个没有评论的 post,严格来说,这会更好。)
您还需要实际单击步骤定义中的“销毁”link - 在步骤定义中的 visit url
之后添加 click_on click_text
。
您还需要将 posts_show_feature.rb
规范中的 "Comment was successfully destroyed." 更改为 "Post was successfully destroyed.",因为这是您现在正在测试的内容。
该测试现在将通过。
但是,您想测试 Post
的 Comment
是否在 Post
被销毁时被销毁,是吗?
这最好在单元测试级别完成:
- 安装rspec-railsgem:
宝石文件:
group :test, :development do
gem 'rspec-rails', '~> 3.5'
end
运行 rails g rspec:install
- 创建以下规范:
spec/models/comment_spec.rb:
require 'rails_helper'
RSpec.describe Comment, type: :model do
let(:post) { Post.create(title: 'title', author: 'author', body: 'body', published_at: DateTime.now) }
it 'is destroyed when its associated Post is destroyed' do
post.comments.create(title: 'title', author: 'author', body: 'body', published_at: DateTime.now)
expect { post.destroy }.to change{ Comment.count }.by(-1)
end
end
有几种方法可以达到该规范,但这是另一次争论 - 这是一种方法。
要使此测试通过(运行 rspec
至 运行 此测试),请向您的 Post
模型添加 'dependent destroy' 声明:
app/models/post.rb:
class Post < ApplicationRecord
has_many :comments, dependent: :destroy
validates :author, presence: true
validates :title, presence: true
validates :body, presence: true
validates :published_at, presence: true
end
这将使您的测试通过!
作为最后的提示,将 --format documentation
放入您的 .rspec
文件中,以使 rspec 打印通过测试(我的个人偏好)。
然后我将构建一个名为 'tests' 的 Rake 任务,它 运行 既是您的 RSpec 规范又是您的 Cucumber 规范,但这不适用于此线程。
我有以下特点:
Scenario: when delete a post
Given I created a post with the title "Test", author "Testauthor" and body "Testbody"
When I browse "/posts" and click on "Destroy"
Then I should see the text "Comment was successfully destroyed."
有问题的代码是这样给定的实现:
Given(/^I created a post with a comment with title "([^"]*)", author "([^"]*)" and body "([^"]*)"$/) do |title, author, body|
post = Post.create! title: 'Test', author: 'Test', body: 'Test', published_at: DateTime.now
Comment.create! title: title, author: author, body: body, published_at: DateTime.now, post_id: post.id
end
由于某种原因 expect(Comment.count).to eq(1)
是假的,没有评论。我做错了什么?
已添加存储库: https://github.com/cwansart/SQ-Hausarbeit/tree/cucumber/features 这是 posts_show.feature 和 posts_show_steps.rb。
我克隆了你的 repo 并看了一下。
Scenario: when delete a post
Given I created a post with the title "Test", author "Testauthor" and body "Testbody"
When I browse "/posts" and click on "Destroy"
Then I should see the text "Comment was successfully destroyed."
posts_show_steps.rb:
Given(/^I created a post with a comment with title "([^"]*)", author "([^"]*)" and body "([^"]*)"$/) do |title, author, body|
post = Post.create! title: 'Test', author: 'Test', body: 'Test', published_at: DateTime.now
Comment.create! title: title, author: author, body: body, published_at: DateTime.now, post_id: post.id
print "#########################"
print Comment.all.pretty_print_inspect
## this shows the comment. It **does** exist...
end
When(/^I browse "([^"]*)" and click on "([^"]*)"$/) do |url, click_text|
print "#########################"
print Comment.all.pretty_print_inspect
## here it doesn't exist any longer...
visit url
end
在这里,你创建了一个post 没有评论,这是第一个问题,即没有要删除的评论!这可以通过将 Given
更改为:
Given I created a post with a comment with title "CommTitle", author "CommAuthor" and body "CommBody"
(也可以重用该步骤定义,但如果此测试只是创建一个没有评论的 post,严格来说,这会更好。)
您还需要实际单击步骤定义中的“销毁”link - 在步骤定义中的 visit url
之后添加 click_on click_text
。
您还需要将 posts_show_feature.rb
规范中的 "Comment was successfully destroyed." 更改为 "Post was successfully destroyed.",因为这是您现在正在测试的内容。
该测试现在将通过。
但是,您想测试 Post
的 Comment
是否在 Post
被销毁时被销毁,是吗?
这最好在单元测试级别完成:
- 安装rspec-railsgem:
宝石文件:
group :test, :development do
gem 'rspec-rails', '~> 3.5'
end
运行 rails g rspec:install
- 创建以下规范:
spec/models/comment_spec.rb:
require 'rails_helper'
RSpec.describe Comment, type: :model do
let(:post) { Post.create(title: 'title', author: 'author', body: 'body', published_at: DateTime.now) }
it 'is destroyed when its associated Post is destroyed' do
post.comments.create(title: 'title', author: 'author', body: 'body', published_at: DateTime.now)
expect { post.destroy }.to change{ Comment.count }.by(-1)
end
end
有几种方法可以达到该规范,但这是另一次争论 - 这是一种方法。
要使此测试通过(运行 rspec
至 运行 此测试),请向您的 Post
模型添加 'dependent destroy' 声明:
app/models/post.rb:
class Post < ApplicationRecord
has_many :comments, dependent: :destroy
validates :author, presence: true
validates :title, presence: true
validates :body, presence: true
validates :published_at, presence: true
end
这将使您的测试通过!
作为最后的提示,将 --format documentation
放入您的 .rspec
文件中,以使 rspec 打印通过测试(我的个人偏好)。
然后我将构建一个名为 'tests' 的 Rake 任务,它 运行 既是您的 RSpec 规范又是您的 Cucumber 规范,但这不适用于此线程。