ruby - rails 4 rspec controllers all failing -
upgrading rails 3.2.17
4.0.1
all rspec's (which passed in 3.2.17
) fail undefined method
, e.g.
failure/error: delete :destroy, id: @group nomethoderror: undefined method `delete' #<rspec::examplegroups::groupscontroller::viewaccess::deletedestroy:0x000000041bc188> failure/error: put :update, id: @group, undefined method `put' #<rspec::examplegroups::groupscontroller::viewaccess::putupdate::invalidattributes failure/error: post :create, group: factorygirl.attributes_for(:group) undefined method `post' ... failure/error: :show, id: @group nomethoderror: undefined method `get' #<rspec::examplegroups::groupscontroller::viewaccess::getshow:0x00000004c5f298>
and lot of
failure/error: session[:user_id] = user.id nameerror: undefined local variable or method `session' #<rspec::examplegroups::groupscontroller::adminaccess::deletedestroy:0x00000004c1ac10>
how fix?
spec is:
require 'spec_helper' describe groupscontroller describe "admin access" before :each @group = factorygirl.create(:group, group_name: 'tools', group_description: 'tools , utilities') user = factorygirl.create(:user, username: 'mdd', password: 'aaa', password_confirmation: 'aaa') session[:user_id] = user.id end describe 'get #index' "populates array of groups" :index assigns(:groups).should eq([@group]) end "renders :index view" :index response.should render_template :index end end describe 'get #show' "assigns requested group @group" :show, id: @group assigns(:group).should == @group end "renders :show template" :show, id: @group response.should render_template :show end end describe 'get #new' "assigns new group @group" :new assigns(:group).should be_a_new(group) end "renders :new template" :new response.should render_template :new end end describe 'get #edit' "assigns requested group @group" :edit, id: @group assigns(:group).should == @group end "renders :edit template" :edit, id: @group response.should render_template :edit end end describe "post #create" context "with valid attributes" "creates new group" expect{ post :create, group: factorygirl.attributes_for(:group) }.to change(group,:count).by(1) end "redirects new group" = post :create, group: factorygirl.attributes_for(:group) response.should redirect_to group.unscoped.last end end context "with invalid attributes" "does not save new group" expect{ post :create, group: factorygirl.attributes_for(:invalid_group) }.to_not change(group,:count) end "re-renders new method" post :create, group: factorygirl.attributes_for(:invalid_group) response.should render_template :new end end end describe 'put #update' context "valid attributes" "located requested @group" put :update, id: @group, group: factorygirl.attributes_for(:group) assigns(:group).should eq(@group) end "changes @group's attributes" put :update, id: @group, group: factorygirl.attributes_for(:group, group_name: "test2", group_description: "test2") @group.reload @group.group_name.should eq("test2") @group.group_description.should eq("test2") end "redirects updated group" put :update, id: @group, group: factorygirl.attributes_for(:group) response.should redirect_to @group end end context "invalid attributes" "locates requested @group" put :update, id: @group, group: factorygirl.attributes_for(:invalid_group) assigns(:group).should eq(@group) end "does not change @group's attributes" put :update, id: @group, group: factorygirl.attributes_for(:group, group_name: "xgroup", group_description: 'xgroup') @group.reload @group.group_name.should_not eq("larry") @group.group_description.should_not eq("smith") end "re-renders edit method" put :update, id: @group, group: factorygirl.attributes_for(:invalid_group) response.should render_template :edit end end end describe 'delete destroy' "deletes group" expect{ delete :destroy, id: @group }.to change(group,:count).by(-1) end "redirects groups#index" delete :destroy, id: @group response.should redirect_to groups_url end end end describe "view access" before :each @group = factorygirl.create(:group, group_name: 'tools', group_description: 'tools , utilities') end describe 'get #index' "populates array of groups" :index assigns(:groups).should eq([@group]) end "renders :index view" :index response.should render_template :index end end describe 'get #show' "assigns requested group @group" :show, id: @group assigns(:group).should == @group end "renders :show template" :show, id: @group response.should render_template :show end end describe 'get #new' "assigns new group @group" :new assigns(:group).should be_nil end "renders :new template" :new response.should redirect_to ladmin_login_url end end describe 'get #edit' "assigns requested group @group" :edit, id: @group assigns(:group).should be_nil end "renders :edit template" :edit, id: @group response.should redirect_to ladmin_login_url end end describe "post #create" context "with valid attributes" "creates new group" expect{ post :create, group: factorygirl.attributes_for(:group) }.to_not change(group,:count) end "redirects new group" post :create, group: factorygirl.attributes_for(:group) response.should redirect_to ladmin_login_url end end context "with invalid attributes" "does not save new group" expect{ post :create, group: factorygirl.attributes_for(:invalid_group) }.to_not change(group,:count) end "re-renders new method" post :create, group: factorygirl.attributes_for(:invalid_group) response.should redirect_to ladmin_login_url end end end describe 'put #update' context "valid attributes" "located requested @group" put :update, id: @group, group: factorygirl.attributes_for(:group) assigns(:group).should be_nil end "changes @group's attributes" put :update, id: @group, group: factorygirl.attributes_for(:group, group_name: "xtools", group_description: "xtools , utilities") @group.reload @group.group_name.should_not eq("xtools") @group.group_description.should_not eq("xtools , utilities") end "redirects updated group" put :update, id: @group, group: factorygirl.attributes_for(:group) response.should redirect_to ladmin_login_url end end context "invalid attributes" "locates requested @group" put :update, id: @group, group: factorygirl.attributes_for(:invalid_group) assigns(:group).should be_nil end "does not change @group's attributes" put :update, id: @group, group: factorygirl.attributes_for(:group, group_name: "xtools", group_description: nil) @group.reload @group.group_name.should_not eq("xtools") @group.group_description.should eq("tools , utilities") end "re-renders edit method" put :update, id: @group, group: factorygirl.attributes_for(:invalid_group) response.should redirect_to ladmin_login_url end end end describe 'delete destroy' "deletes group" expect{ delete :destroy, id: @group }.to_not change(group,:count) end "redirects groups#index" delete :destroy, id: @group response.should redirect_to ladmin_login_url end end end end
rspec 2 guessing whether given class controller or model based on spec file location. guessing disabled default in rspec 3, need add following line spec_helper (or rather rails_helper) enable it:
config.infer_spec_type_from_file_location!
alternatively need tell rspec testing with:
describe groupscontroller, type: :controller
Comments
Post a Comment