简体   繁体   中英

rails rspec model and controller testing

I'm just wondering if I understend test in a proper way.

The model tests should be done without mocking, eg.:

rspec

model.name = 'test'
model.save
model.should eq('test')

and the controllers should be based on mocking:

rspec

model.should_receive(:save).and_return(true)

controller

def action
...
if model.save
...
end

Summing up: controllers are tested without any true data. All data are "provide" by stubs and mocks in contrast to model layer which operates on ... db?

but I assume that model also should be mocked

model.name = 'test'
model.should_receive(:save)
model.should eq('test')

but I dont see sense of testing like this because I don't test the save method.

As a general rule of thumb that is how I go about it.

Using your example, if you have tested that the save method in your model spec, you don't need to test it again in the controller, all you need to know is that it's called.

Essentially you need to test the behaviour of the controller, not how the model reacts to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM