Hi Misha,

belongs_to/has_many is the ideal here.

 it "should have the right post type"
    post_type = PostType.first
    post = Post.create(@attr.merge(:post_type => post_type))
    post.post_type.should == post_type # or  post.post_type_id.should ==
post_type.id
    post.errors.should be_empty?   # not sure about the Rspec syntax here,
something like that though
 end

If you want to add validations, then you can also write a spec to create the
post without the post_type, try saving, and add an assertion that an error
about empty post_type shows up.

I would look into FactoryGirl for database records instead of fixtures. We
use it here at Sony, and it works amazingly.

Personally, I wouldn't test the associations on the model too much. They are
pretty well tested on the Rails end. I would test the controller actions
that assign params to the object though and save it (in a functional test
for the controller), because that is code you will have to write, to make
sure it goes the full stack. That would be a more useful test. But if you
are just trying to learn, then this is a good exercise, what you are doing
here.

Resources:
http://guides.rubyonrails.org/
http://railsforzombies.org/
http://www.codeschool.com/courses/rails-best-practices
railscasts.com


Hope that helps,
Adam

On Mon, Oct 17, 2011 at 11:06 PM, misham <[email protected]> wrote:

> 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
>

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

Reply via email to