I'm trying to get a handle on testing controllers, and so far I seem to be stuck on the simplest problems.
documents.controller:
def edit
@document = Document.find(params[:id])
end
documents_controller_spec:
describe 'GET #edit', focus: true do
before(:each) { @doc = FactoryGirl.create(:document)}
it "should assign @document to the document" do
get :edit, id: @doc
assigns(:document).should eq(@doc)
end
end
Always returns false. @document is always assigned nil. I've tried specifying params[id] to be @doc.id, but that doesn't fix anything. What am I doing wrong here?
From rspec's perspective, the two documents (assigned to @doc) you are comparing in the line:
assigns(:document).should eq(@doc)
are not the same - they have the same ID in the database, but the actual ruby objects you are comparing are different, as one was created in your 'before' block, while the other is presumably instantiated inside your controller when the GET request executes in your test.
My guess is the following will work:
assigns(:document).id.should eq(@doc.id)
Try changing
get :edit, id: @doc
to
get :edit, id: @doc.id
Basically, it just needs the id to build the route.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.