gpt4 book ai didi

ruby-on-rails - Nil 不是 Rspec 中的符号

转载 作者:行者123 更新时间:2023-12-04 05:47:23 24 4
gpt4 key购买 nike

当我在本地主机上以开发模式运行时。一切正常,我可以轻松删除我的帖子。但是我的请求规范失败了。

我在运行测试时收到此错误:

1) Posts when logged in should delete posts
Failure/Error: expect{click_on "Usuń spot"}.to change(Post.count).by(1)
TypeError:
nil is not a symbol

post_spec.rb
describe "when logged in" do
let(:user) {FactoryGirl.create(:user)}
let!(:post) {user.posts.create(content: "Brilliant! I just saw the most amazing ever. She looked so cute!")}
content = "Example of spot post, for TDD. It's not real spot. Not yet."

before(:each) {
log_user(user)
}

it "should delete posts" do
visit user_post_path(user, post)
expect{click_on "Usuń spot"}.to change(Post.count).by(1)
end

显示.haml
=@post.content
=link_to "Usuń spot", [@user,@post], method: :delete

post_controller.rb
  def show
@user = current_user
@post = @user.posts.find(params[:id])
end

def destroy
@user = current_user
@post = @user.posts.find_by_id(params[:id])
@post.destroy

flash[:success] = "Post destroyed"

redirect_to root_url
end

最佳答案

change需要一个块,因此可以对其进行两次评估以验证值的变化。

这应该有效:

expect{click_on "Usuń spot"}.to change{Post.count}.by(1)

关于ruby-on-rails - Nil 不是 Rspec 中的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16131623/

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