jeudi 25 août 2016

Rspec: Creating user before testing method

Hi I am new to rspec (and unit testing in general) and want to test the following method:

class HelloController < ApplicationController

  def hello_world
    user = User.find(4)
    @subscription = 10.00
    render :text => "Done."
  end
end

I am trying to use Rspec like so:

Describe HelloController, :type => :controller do

    describe "get hello_world" do

      it "should render the text 'done'" do
        get :hello_world
        expect(response.body).to include_text("Done.")
      end
    end
  end

I would like to simply test that the method works properly and renders the test "done". I get the following error when I run the test:

 Failure/Error: user = User.find(4)

 ActiveRecord::RecordNotFound:
   Couldn't find User with 'id'=4

But how do I properly create a user with that id before executing it? I have tried the following based on other tutorials and questions but it doesn't work:

describe "get hello_world" do
        let(:user) {User.create(id: 4)}

            it "should render the text 'done'" do
                get :hello_world
                expect(response.body).to include_text("Done.")
            end
    end

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire