Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model testing with rspec

Tags:

rspec

I have a piece of code that I want to test:

class User < ActiveRecord::Base

 has_many :servers, :dependent=> :destroy

end

Please give an example of RSpec code that tests the association code.

like image 926
Arun Sharma Avatar asked Dec 24 '10 07:12

Arun Sharma


People also ask

Is RSpec used for unit testing?

RSpec is a unit test framework for the Ruby programming language. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool. What this means is that, tests written in RSpec focus on the "behavior" of an application being tested.

Is RSpec TDD or BDD?

RSpec is a Behavior-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.

What is RSpec testing?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.


1 Answers

For me this variant works well:

describe User do
  it { should have_many(:servers).dependent(:destroy) }
end
like image 164
ka8725 Avatar answered Sep 18 '22 16:09

ka8725