0

我有以下 create 方法,它创建一个新的活动数组,循环活动的参数,将 设置action_type为 parameter action_type。然后使用action_type. 然后我将局部变量活动推送到@activities数组中,然后为评论和警报创建一个新实例。

def create
  @activities = []
  params[:activities].each do |act|
    action_type = act[:action_type]
    activity = Activity.create(action_type: action_type)
    @activities << activity
    Comment.create(comment_text: act[:comment])
    Alert.create(alert_id: act[:alert_id])

  end
  render json: @activities
end

当我尝试运行以下 RSpec 测试时出现我的问题

 let(:valid_hash_attributes) do
    [
      {
        action_type:"Right-sized next generation application",
        entered_by:"Mrs. Cortney Goyette",
        alert_id:1,
        comment:"This is a acomment"
      }
    ]
   end

  let(:valid_session) { {} }

  describe "POST create" do
    describe "with valid params" do
      it "creates a new Activity" do
        expect {
          post :create, {:activities => valid_hash_attributes}, valid_session
        }.to change(Activity, :count).by(1)
      end

      it "assigns a newly created activity as @activity" do
        post :create, {:activities => valid_hash_attributes}, valid_session
       assigns(:activity).should be_a(Activity)
        assigns(:activity).should be_persisted
      end
    end
  end
end

我得到以下输出:

NoMethodError: 未定义的方法each' for nil:NilClass ./app/controllers/activities_controller.rb:30:in创建'./spec/controllers/activities_controller_spec.rb:87:in `(root)'

真正奇怪的是,在 RSpec 日志中显示我们创建的这些对象如下所示:

参数:{"activities"=>[{"action_type"=>"合适大小的下一代应用程序", "entered_by"=>"Mrs. Cortney Goyette", "alert_id"=>"1", "comment"=> "这是一条评论"}]} Savepoint (0ms) SAVEPOINT active_record_1 SQL (1.0ms) INSERT INTO "activities" ("action_type", "created_at", "updated_at", "user_id") VALUES ('Right-sized next generation application', '2013-10-18 13:17:46.129000', '2013-10-18 13:17:46.129000', 1) Savepoint (0ms) RELEASE SAVEPOINT active_record_1 (0ms) SELECT name FROM sqlite_master WHERE type = 'table ' AND name = "comments" Savepoint (0ms) SAVEPOINT active_record_1 SQL (1.0ms) INSERT INTO "评论”(“comment_text”、“created_at”、“updated_at”)值('这是一个评论'、'2013-10-18 13:17:46.212000'、'2013-10-18 13:17:46.212000') Savepoint (1.0ms) RELEASE SAVEPOINT active_record_1 (1.0ms) SELECT name FROM sqlite_master WHERE type = 'table' AND name = "alerts" Savepoint (0ms) SAVEPOINT active_record_1 SQL (1.0ms) INSERT INTO "alerts" ("alert_id", " created_at", "updated_at") VALUES (1, '2013-10-18 13:17:46.332000', '2013-10-18 13:17:46.332000') 保存点 (0ms) 释放保存点 active_record_1212000')保存点(1.0ms)释放保存点active_record_1(1.0ms)从sqlite_master选择名称,其中类型=“表”和名称=“警报”保存点(0毫秒)保存点活动记录_1 SQL(1.0毫秒)插入“警报”(“alert_id” ", "created_at", "updated_at") 值 (1, '2013-10-18 13:17:46.332000', '2013-10-18 13:17:46.332000') 保存点 (0ms) 释放保存点 active_record_1212000')保存点(1.0ms)释放保存点active_record_1(1.0ms)从sqlite_master选择名称,其中类型=“表”和名称=“警报”保存点(0毫秒)保存点活动记录_1 SQL(1.0毫秒)插入“警报”(“alert_id” ", "created_at", "updated_at") 值 (1, '2013-10-18 13:17:46.332000', '2013-10-18 13:17:46.332000') 保存点 (0ms) 释放保存点 active_record_12013-10-18 13:17:46.332000') 保存点 (0ms) 释放保存点 active_record_12013-10-18 13:17:46.332000') 保存点 (0ms) 释放保存点 active_record_1

实际上很困惑为什么这个测试没有通过。

4

1 回答 1

0

您的第二个规范正在传递valid_attributes给未定义的控制器(因此为零)。应该是valid_hash_attributes

编辑:根据您在 IRC ( http://i.imgur.com/MTDgc4T.png ) 上发布的屏幕截图,您还使用了不正确的哈希键 - 您的第二个测试是传入activity参数,不是activities,因此params[:activities]是 nil .

于 2013-10-18T14:22:26.117 回答