Rspec ArgumentError,未定义的对象

时间:2017-07-20 01:05:19

标签: ruby-on-rails ruby rspec

为什么我收到以下错误? 在控制台中运行rspec测试时,我收到错误消息' post'不明。然而,失败的代码行是这样构造的:

def getCellValue(Cell cell){
    def result
    switch (cell.getCellType()) {
        case Cell.CELL_TYPE_FORMULA:
            // if(cell.getCellType() == Cell.CELL_TYPE_ERROR){
            //  result = 0
            // } else {
            //  result = cell.getNumericCellValue()
            // }
            ////////////////////////////////////////////////

            result = cell.getNumericCellValue()//i need a condition for this case

            break
        case Cell.CELL_TYPE_NUMERIC:
            result = cell.getNumericCellValue()
            break
        case Cell.CELL_TYPE_STRING:
            result = cell.getStringCellValue()
            break
        case Cell.CELL_TYPE_BOOLEAN:
            result = cell.getBooleanCellValue()
            break
        case Cell.CELL_TYPE_BLANK:
            result = null
            break
        default:
            result = cell.toString()
            break
    }

    return result
}

因此:

post :create, valid_params

这是错误

it "assigns the new post to @post" do
  post :create, post: {title: RandomData.random_sentence, body: RandomData.random_paragraph}
  expect(assigns(:post)).to eq Post.last
end

1 个答案:

答案 0 :(得分:1)

如果您正在使用Rails 5,那么您需要将post对象包含在params关键字参数中,请尝试:

post :create, params: { post: { title: RandomData.random_sentence, body: RandomData.random_paragraph } }