嗨,我是 ruby on rails 的新手。
目前我在使用 rspec 和 FactoryGirl 在 Rails 上测试控制器时遇到问题。我正在尝试测试 activerecord 是否与我的动作“显示”相对应,但它没有通过或收到任何内容。我能做些什么来解决这个问题吗?谢谢你
我得到的错误是:
*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*
这是我的代码
level_access_controller.erb:
上下文'需要实例'做
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
仅供参考:我也已经为就业做了一个实例。
工厂/level_access.erb:
FactoryGirl.define 做
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
结尾