Hi,

I'm trying to figure out how to test a relationship between two models
using RSpec and Rails 3.1

I included a RSpec file with a sample test that passes.  I'm trying to
write a test that will validate assignment of one of the Post Types
and check that it was assigned correctly.  This testing the same
behavior as picking a Post Type using a drop-down menu and having it
saved with the new Post.

* How would I write the last test in the sample RSpec file below?
* Is there a good tutorial or resource on testing this kind of
behavior?
* Is this a good way to represent this association? Would has_one/
has_many be better instead of belongs_to/has_many?

Thank you

- Misha

-------------
Code:

The sample data in the Post Types is:

id | value | ...
1  | foo    | ...
2  | bar    | ...
3  | baz   | ...


# app/models/post_type.rb
class PostType < ActiveRecord::Base
    attr_reader :name

    has_many :posts
end

# app/models/post.rb
class Post < ActibeRecord::Base
    attr_accessible :content

    belongs_to :post_type
end

# spec/models/post_spec.rb
describe Post do
  before :each do
    @attr = { :content = "Lorem ipsum" }
  end

  # ... other tests

  describe "post type association" do
    before :each do
      @post = Post.create @attr
    end

    it "should have a post type attribute" do
      @post.should respond_to :post_type
    end

    it "should have the right post type"
  end
end

-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to