N
N
Neznaikin2015-10-29 15:27:17
Ruby on Rails
Neznaikin, 2015-10-29 15:27:17

Testing nested resources?

I started to study BDD on the example of Stackoverflow, I got to testing the features of answers resources. Actually, this is the question - how to test nested resources, if you need to test according to the rules only within one controller?
How to test an answers controller that has the path question/(;question_id)/answer(:id)
Sorry if the question is confusing. I’m still learning, and I don’t understand terminology especially, but I don’t understand at all ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
raventid, 2015-10-29
@Neznaikin

As far as I understand you have an AnswersController controller? And he manages the responses resource. If so, then yes, write controller tests for it in the answers_controller_spec.rb file, for example, creating using Ajax can be tested like this
answers_controller_spec.rb

describe 'POST #create' do
    sign_in_user

    context 'with valid attributes' do
      it 'saves answer in database' do
        expect { post :create, question_id: question, answer: attributes_for(:answer), format: :js }.to change(question.answers, :count).by(1)
      end

      it 'assigns answer with current user' do
        post :create, question_id: question, answer: attributes_for(:answer), format: :js
        assigning_answer = assigns(:answer)
        expect(assigning_answer.user_id).to eq subject.current_user.id
      end

      it 'render create template' do
        post :create, question_id: question, answer: attributes_for(:answer), format: :js
        expect(response).to render_template :create
      end
    end

And don't forget to pay special attention to acceptance specs and model tests. Controller tests are not the most important thing :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question