StatusCode: Our Weekly Programming Newsletter. You're not just a Rubyist, you're a software developer, right? :-) Check out StatusCode Weekly, our language and architecture agnostic newsletter that covers the biggest news across the entire software development world.

RubyFlow The Ruby and Rails community linklog

×

The Ruby and Rails community linklog

Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!

Are you testing your attr_* methods?

If you do, then you might want to clean up your specs with RSpec attr extensions.

Comments

No, I’m not testing my attr_* methods. They are an implementation detail at not an expression of behavior.

I don’t know enough rspec to convert it myself, but model.instance_methods.should include(field.to_s) is slow code. You should use method_defined? instead of instance_methods.include? (I’d assume: model.should method_defined(field))

And on another note: while I do test my attr_* methods, I don’t particularly care that those methods have been defined as attr_*, they’re just methods. So I don’t treat them differently.

@bryanl attr_*, by creating methods, creates behavior in your class. and if you don’t test them, then someone may be able to delete them from your code, breaking your app, yet still causing your tests to pass.

@apeiros thanks for pointing our method_defined?. as for your comment about attr_* methods not being different from any other method. you’re right. but if i need to test those methods and they don’t do anything “special” like caching or validating the input, i really don’t see the point of writing the same specs over and over.

@rubiii you’re welcome. The second comment was more geared towards bryanl. Though you could beef up attr_accessor by testing the following:


random_value = generate_some_random_value
x.foo = random_value
x.should be(random_value)

Argh, that should have been x.foo.should be(random_value), of course :-(

No. Don’t test those!

Test them indirectly if you can, but make sure they are tested somehow!

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in