Question

I am working on a rails project, in which model queries a service and gets the data from the service. Now, when I want to write tests for models and controllers I need to imitate the service.I am fairly new to rails testing. I am confused as to what I should do. Rails has stubs, mocks and fixture - which of the three should I use. I suppose fixtures wont be useful. Suggestion plz . Thanks.

No correct solution

OTHER TIPS

Most Rails tests don't need stubs or mocks, but in this context, an example where you might need it is to mock a model in a controller or integration test.

Since controllers typically manipulate models, you will sometimes want to verify the model has been called in a certain way. You can usually do this just by asserting against the model's state, after the controller has been invoked, but occasionally you may wish to verify the actual call made from controller to model. In this case, you "mock" the model using a Ruby mocking library and setup the expectation prior to calling the controller. The test will fail if the expected model method wasn't called.

Another motivation may be to fake some data being returned, in which case you would "stub" the model. Stubbing is just mocking without any kind of assertion, you can prepare some fake behaviour.

More info on Rails stubbing and mocking

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top