Chris Olsen
2007-Dec-11 17:28 UTC
[rspec-users] can''t access helper methods for model testing
I would like to have some valid mock models readily available in the
helper module, but when I try to access the helper I get the error
saying that the method is not found.
======describe MembersHelper do # Helper methods can be called directly in the
examples (it blocks)
def valid_member_mock
member = mock_model(Member)
member.stub!(:first_name).and_return("Joe")
member.stub!(:last_name).and_return("Smith")
member.stub!(:email).and_return("joe at smith.com")
member.stub!(:city).and_return("LA")
member.stub!(:region).and_return("CA")
member.stub!(:region_code).and_return("234234")
return member
end
end
======require File.dirname(__FILE__) + ''/../spec_helper''
describe Member do
before(:each) do
@member = valid_member_mock
end
it "should be valid" do
@member.should be_valid
end
end
======NameError in ''Spec::Rails::Example::ModelExampleGroup Member
should be
valid''
undefined local variable or method `valid_member_mock'' for
#<Spec::Rails::Example::ModelExampleGroup::Subclass_1:0x20e6814>
======
I would assume that rails is including the helper module behind the
scenes, although I have explicitly made the require with no luck.
What am I missing? Are these helpers only for controller tests?
Thanks
--
Posted via http://www.ruby-forum.com/.
Chris Olsen
2007-Dec-11 17:33 UTC
[rspec-users] can''t access helper methods for model testing
This isn''t the best example, as I normally wouldn''t validate a mock model, it was more for demonstration, although a bad one. -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2007-Dec-12 05:09 UTC
[rspec-users] can''t access helper methods for model testing
On Dec 11, 2007 11:28 AM, Chris Olsen <lists at ruby-forum.com> wrote:> I would like to have some valid mock models readily available in the > helper module, but when I try to access the helper I get the error > saying that the method is not found. > ======> describe MembersHelper do # Helper methods can be called directly in the > examples (it blocks) > def valid_member_mock > member = mock_model(Member) > member.stub!(:first_name).and_return("Joe") > member.stub!(:last_name).and_return("Smith") > member.stub!(:email).and_return("joe at smith.com") > member.stub!(:city).and_return("LA") > member.stub!(:region).and_return("CA") > member.stub!(:region_code).and_return("234234") > > return member > end > end > ======> require File.dirname(__FILE__) + ''/../spec_helper'' > > describe Member do > before(:each) do > @member = valid_member_mock > end > > it "should be valid" do > @member.should be_valid > end > end > ======> NameError in ''Spec::Rails::Example::ModelExampleGroup Member should be > valid'' > undefined local variable or method `valid_member_mock'' for > #<Spec::Rails::Example::ModelExampleGroup::Subclass_1:0x20e6814> > ======> > I would assume that rails is including the helper module behind the > scenes, although I have explicitly made the require with no luck.You know the old saying about assuming. When you assume ... you should write an example!> > What am I missing? Are these helpers only for controller tests?Rails helpers are there for views and controllers, not for models. If you want an RSpec helper, something you use to set up state for your examples, you can write a module and include it in the example groups - but that is a horse of a different color. HTH, David> > Thanks > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Chris Olsen
2007-Dec-14 06:43 UTC
[rspec-users] can''t access helper methods for model testing
>> What am I missing? Are these helpers only for controller tests? > > Rails helpers are there for views and controllers, not for models. > > If you want an RSpec helper, something you use to set up state for > your examples, you can write a module and include it in the example > groups - but that is a horse of a different color. > > HTH, > DavidThat makes sense. Thanks for the help. -- Posted via http://www.ruby-forum.com/.