2

在 Railstutorial(.org) 的 Rails 3.2 版本的第11.2.5节中,我无法通过所有的关系控制器规范 rb 测试,即用于创建和破坏关系的“应该成功响应”方法。尽管使用了所提供代码的精确副本(复制和粘贴),但仍然如此。收到的消息是expected success? to return true, got false

如果它在某种程度上相关,我的关注/取消关注按钮没有在网络浏览器中更新,但我能够通过= require jquery在 app/assets/javascripts/application.js 中激活来解决这个问题(尽管这一步似乎不是教程文本中的任何地方都提到过)。在此修复之前,未呈现关联的 create.js.erb 和 destroy.js.erb 操作。

现在应用程序似乎正在运行,包括 ajaxified 的关注/取消关注按钮(它们都在屏幕上更新并按预期修改关系)。尽管如此,提到的两个 rspec 测试仍然失败。我已经分析了服务器日志,但到目前为止还没有给我任何线索。

关系控制器规范.rb

require 'spec_helper'

describe RelationshipsController do

  let(:user) { FactoryGirl.create(:user) }
  let(:other_user) { FactoryGirl.create(:user) }

  before { sign_in user }

  describe "creating a relationship with Ajax" do

    it "should increment the Relationship count" do
      expect do
        xhr :post, :create, relationship: { followed_id: other_user.id }
      end.to change(Relationship, :count).by(1)
    end

    it "should respond with success" do
      xhr :post, :create, relationship: { followed_id: other_user.id }
      response.should be_success
    end
  end

  describe "destroying a relationship with Ajax" do

    before { user.follow!(other_user) }
    let(:relationship) { user.relationships.find_by_followed_id(other_user) }

    it "should decrement the Relationship count" do
      expect do
        xhr :delete, :destroy, id: relationship.id
      end.to change(Relationship, :count).by(-1)
    end

    it "should respond with success" do
      xhr :delete, :destroy, id: relationship.id
      response.should be_success
    end
  end
end

关系控制器.rb

class RelationshipsController < ApplicationController
  before_filter :signed_in_user

  def create
    @user = User.find(params[:relationship][:followed_id])
    current_user.follow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end

  def destroy
    @user = Relationship.find(params[:id]).followed
    current_user.unfollow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end
end

可以在https://github.com/spinnn/testapp找到应用程序仓库

我已经注意到

按钮在 Ajax - Rails 教程 3 中未更新,第 12.2.5 节

但是这个讨论似乎不适用于这种情况(它也与教程的 Rails 3.0 而不是 Rails 3.2 版本有关)。

我已经采取了多次重新启动 Web 服务器以及重置、重新填充和重新准备数据库的步骤,但问题没有得到任何解决。

我在 OS X 上使用 ruby​​ 1.9.3p286 和 Rails 3.2.8。任何有关解决此问题的建议将不胜感激。

4

1 回答 1

0

好吧,是时候害羞了。克隆 railstutorial/sample_app_2nd_ed.git 之后,我找到了一个非常直接的解决方案来解决这个问题。relationship_controller.rb 已复制,在 app/controllers/ 中有一个副本,在 spec/controllers/ 中也有一个(冗余)副本 不幸的是,我一直在尝试调试的版本是冗余版本,而实际版本不完整。掌心。也许是警示故事。

于 2015-03-31T06:08:46.977 回答