My application has two models: User and Employee, and their relation is user has_many employees.
As I trying to write a Rspec test case for the employee controller:
describe "GET 'edit'" do
it "should get user/edit with log in" do
log_in(@user)
employee = mock_model(Employee, :id=>1, :user_id=>@user.id)
get :edit, :id=>employee
response.should be_success
end
end
I got the result as:
....F Failures: 1) EmployeesController GET 'edit' should get user/edit with log in Failure/Error: get :edit, :id=>employee Mock "Employee_1" received unexpected message :to_ary with (no args) # C:in `find' # ./app/controllers/employees_controller.rb:41:in `edit' # ./spec/controllers/employees_controller_spec.rb:51:in `block (3 levels) in ' Finished in 4.31 seconds 5 examples, 1 failure
Can someone help me out with this please? Thanks
I think the problem is you are using mock_model, which will throw errors if you call a method on the model that you are not explicitly creating an expectation for. In this case, you could create you employee with stub_model since you are performing no checks on calls actually made to the model.
describe "GET 'edit'" do
it "should get user/edit with log in" do
log_in(@user)
employee = stub_model(Employee, :id=>1, :user_id=>@user.id)
get :edit, :id=>employee
response.should be_success
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With