-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSPEC for models
131 lines (91 loc) · 3.53 KB
/
RSPEC for models
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
sample code rspec controller:
-----------------------------
before(:each) do
@question = "Define vote"
@answer = "express one's perference for a candidate"
end
it "should question and answer redirect to the show page" do
post :admin_new, :question => @question, :answer => @answer
response.should redirect_to(:controller => "post", :action => "admin_show")
response.code.should eq("302")
end
it "should question and answer create successfully means redirect to the index page" do
post :admin_create
blog = Blog.create(:id => 1,:question => "What is our national flower",:answer => "lotus", :created_at => "",:updated_at => "")
response.should render_template(:controller => "post", :action => "admin_new")
response.should be_successful
end
it "should update the created Sample application record" do
question = "Define vote"
answer = "Express one's perference for a candidate"
response.code.should eq("200")
end
it "should update the Record's" do
blog = Blog.update_attribute(:id => 1,:question => "What is our national animal",:answer => "lion", :created_at => "",:updated_at => "") rescue nil
response.should be_success
end
end
=================
On Sat, Jan 7, 2012 at 7:00 PM, Anupriya D <[email protected]> wrote:
https://github.com/thoughtbot/shoulda - refer this for validation
And use rake db:migrate && rake db:test:prepare ->to create a test db.
Sample controller:
------------------
def dashboard
@features_deals=Deal.featured_deals
@child = Child.find(:first,:conditions=>["user_id=?",current_user.id])
@cashout = ChildCashout.new
end
Sample Controller for spec:
---------------------------
describe ChildrenController do
describe "dashboard" do
it "should be successful" do
get 'dashboard'
@child=Factory(:child)
@cashout = ChildCashout.new
end
end
end
On Sat, Jan 7, 2012 at 5:48 PM, Anupriya D <[email protected]> wrote:
Hi All,
Ref Sites:
---------
http://relishapp.com/rspec
http://rubydoc.info/gems/rspec-rails/frames
Required Gems and Installation Steps:
------------------------------------
1.Add the below gems in Gem file
gem "capybara"
gem 'rspec'
gem 'rspec-rails'
gem "shoulda"
3) bundle install ~
4) rails g rspec:install => It will create a spec folder .
5) rspec spec => for running the spec
6) To run all the spec use rake spec command
7)To run particular model bundle exec rspec spec/models/model_name similarly for controller/views/helpers.
Sample code for model:
----------------------
has_many :attachments
has_many :payments
has_many :comments
has_one :child
has_one :parent
has_one :gaurdian
validates :first_name,:presence=>{:message=>"Please enter your First Name"}
validates :last_name,:presence=>{:message=>"Please enter your Last Name"}
Sample code for model spec:
---------------------------
it { should have_one(:child) }
it { should have_one(:parent) }
it { should have_one(:gaurdian) }
it { should have_many(:attachments) }
it { should have_many(:payments) }
it { should have_many(:comments) }
it { should validate_presence_of(:email)}
it { should validate_presence_of(:password)}
it { should validate_presence_of(:first_name).with_message("Please enter your First Name")}
it { should validate_presence_of(:last_name).with_message("Please enter your Last Name")}
Regards,
Anu