Rails最小,关系不起作用

时间:2015-03-11 12:05:48

标签: ruby-on-rails ruby minitest

我正在尝试创建一个检查是否可以创建答案的测试。答案只能存在于问题之下。这是我测试的代码:

test "should be able to create answer" do
    answer = answers(:answer1)
    assert_difference("Answer.count") do
        post :create, answer: {body: answer.body, question: answer.question}
    end
    assert_equal "Je vraag is ingedient!", flash[:notice], "Incorrect flash notice"
end

ASD

在控制器中有一个重定向到答案的父问题:

redirect_to @answer.question

但它重定向到nil,因为没有问题因此没有父母:

ActionControllerError: Cannot redirect to nil!

我如何为这种情况编写测试?

夹具: answers.yml

answer1:
  body: Practice in in front of your parents
  question: question1

questions.yml

question1:
  title: What is the best way to prepare a presentations in another      language?
  body: I have to pitch in my class.

1 个答案:

答案 0 :(得分:0)

我必须在创建这样的答案之前设置问题:

test "should be able to create question" do
    question = questions(:question1)
    assert_difference("Question.count") do
        post :create, question: {title: question.title, body: question.body}
    end
    assert_redirected_to question_path(assigns(:question)), "Not redirected to question"
    assert_equal "Je vraag is ingedient!", flash[:notice], "Incorrect flash notice"
end