Rspec and Rails with View and Nested Resources

I am working on a Yahoo Answers app to improve my Rails skills. So far, I have installed two Question and Answer models, and they are nested in this way:

  resources :questions do
    resources :answers
  end

I did tests for controllers, models, and question views, but I have minor problems presenting answers and nested routes. I use Rspec and Factory girl.

I have the following test:

describe "answers/new.html.erb" do
  before(:each) do
    @question = Factory(:valid_question)
    @answer = Factory(:valid_answer)
    assign(:question, @question)
    assign(:answer, stub_model(Answer,
      :text => "MyString",
      :question_id => 1
    ).as_new_record)
  end

  it "renders new answer form" do
    render
    assert_select "form", :action => question_answers_path(@question), :method => "post" do
      assert_select "textarea#answer_text", :name => "answer[text]"
      assert_select "input#answer_question_id", :name => "answer[question_id]"
    end
  end
end

and whenever I run the test, I get the following message:

  3) answers/new.html.erb renders new answer form
     Failure/Error: render
     ActionView::Template::Error:
       No route matches {:controller=>"answers"}
     # ./app/views/answers/new.html.erb:6:in `_app_views_answers_new_html_erb__3175854877830910784_6513500'
     # ./spec/views/answers/new.html.erb_spec.rb:16:in `block (2 levels) in <top (required)>'

I have tried many things how to do

render new_question_answer_path(@question)

but I get this:

  3) answers/new.html.erb renders new answer form
     Failure/Error: render new_question_answer_path(@question.id)#, :format=>:html
     ActionView::MissingTemplate:
       Missing partial /questions/1/answers/new with {:handlers=>[:erb, :builder, :coffee], :formats=>[:html, :text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :
url_encoded_form, :json], :locale=>[:en, :en]}. Searched in:
         * "/home/juan/rails_projects/answers/app/views"
     # ./spec/views/answers/new.html.erb_spec.rb:16:in `block (2 levels) in <top (required)>'

Could you help me with this? I'm a little impolite now.

+5
source share
2 answers

I think the mistake is in your opinion. Can you add it?

, RSpec:

  • @question @answer let. . , .
  • FactoryGirl.create, Factory(). create, Factory::Syntax::Methods RSpec.
  • , . , - stub_model Answer.build, @question @answer. FactoryGirl Factory.build_stubbed, stub_model .
  • - . RSpec , . , ( ) - . , , . , . , , rspec capybara.
  • - , . , , , , , . . , - , , .
+8

. , , , 6.

rails, - answers_path(@question), .

, assign . , :locals render.

+2

All Articles