Hi i'm new on ruby on rails.
Currently i'm having a problem testing controller on rails using rspec and FactoryGirl. I'm trying to test activerecord for corresponding with my action 'show' but it didn't get through or received any. Is there anything that i can do to fix this? thank you
the error i got is :
*Failure/Error: Employment::LevelAccess.should_receive(:find).with(@level_access.id.to_s).and_return(@level_access)
(<Employment::LevelAccess(id: integer, employment_id: integer, accessiblity_id: integer, actions: string, user_id: integer, created_at: datetime, updated_at: datetime) (class)>).find("1")
expected: 1 time
received: 0 times*
Here are my codes
level_access_controller.erb :
context 'require instance' do
before do
@level_access = FactoryGirl.create(:level_access_2, employment_id: @employment.id)
end
describe 'Get id to Show' do
it "should find the correct value to show" do
Employment::LevelAccess.should_receive(:find).with(@level_access.id.to_s).and_return(@level_access)
get :show, :employment_id => @employment.id, :id => @level_access.id
end
end
describe 'Get show' do
before do
get :show , :employment_id => @employment.id, :id => @level_access.id
end
it "should render a show page" do
expect(response).to render_template("show")
end
it "should not redirect to any page" do
response.should_not redirect_to("hahahha")
end
end
end
FYI: i also already make an instance for the employment as well.
Factories/level_access.erb :
FactoryGirl.define do
factory :level_access, class: Employment::LevelAccess do
employment_id {FactoryGirl.create(:employment).id}
actions 'create,update,new,destroy,show'
end
factory :level_access_2, class: Employment::LevelAccess do
actions 'show'
end
end
I finally figure it out by asking help from my project manager
he quoted
LevelAccessController :
class Manage::Employments::LevelAccessesController < InheritedResources::Base before_filter :authenticate_user! defaults :resource_class => Employment::LevelAccess, :collection_name => 'level_accesses', :instance_name => 'level_access'
protected
def begin_of_association_chain
employment
end
def employment
@employment ||= Employment.find(params[:employment_id])
end
end
so i change the rspec which describe 'Get id to Show' do
it "should find the correct value to show" do
Employment::LevelAccess.should_receive(:find).with(@level_access.id.to_s).and_return(@level_access)
get :show, :employment_id => @employment.id, :id => @level_access.id
end
end
into
it "should find the correct value to update" do
@employment.level_accesses.should_receive(:find).with(@level_access.id.to_s).and_return(@level_access)
put :update, :employment_id => @employment.id, :id => @level_access.id
end
since the begin_of association_chain relation in controller should have make the instance available for relation chaining in the rspec.