Reputation: 684
I'm trying to learn rspec. I can't seem to test a rails controller method. When I call the method in the test, rspec just returns an undefined method error. Here is my test example
it 'should return 99 if large' do
GamesController.testme(1000).should == 99
end
and here is the error:
Failure/Error: GamesController.testme(1000).should == 99
NoMethodError:
undefined method `testme' for GamesController:Class
I do have a testme method in the GamesController. I don't understand why the test code cannot see my methods.
Any help is appreciated.
Upvotes: 23
Views: 25761
Reputation: 2336
I think the correct way is this:
describe GamesController do
it 'should return 99 if large' do
controller.testme(1000).should == 99
end
end
In a rails controller spec, when you put the controller class in describe
, you can use controller
method to get an instance :P
Obviously, if testme
method is private, you still have to use controller.send :testme
Upvotes: 40
Reputation: 10038
You try to test class method, but controller has instance method
You need GamesController.new.testme(1000).should == 99
Or even GamesController.new.send(:testme, 1000).should == 99
, because, as I think, this is not action method, but private or protected.
Action methods are tested this way
Upvotes: 8