这个项目是
https://github.com/chucai/Ruby-on-Rails-Tutorial-by-Michael-Hartl--v3.2-
有比较网站的Rails测试代码,项目比较小,只有三张表,但是代码结构简单,思路清晰,有非常多的值得借鉴和学习的地方。
所有做了这个研究笔记。
1.实现的功能有:
•集成了cucumber和rspec测试
•自己定义的用户认证[没有使用devise]
•简单的微博类型的跟随
•简单的micropost提交
•使用了bootstrap框架
这个小系统所具备的功能有:
1,登录与注册
2,跟随某人
3,个人首页
4,写微博
2.如何在where中使用占用符?
private
#ReturnsanSQLconditionforusersfollowedbythegivenuser.
#Weincludetheuser'sownidaswell.
defself.followed_by(user)
followed_user_ids=%(SELECTfollowed_idFROMrelationships
WHEREfollower_id=:user_id)
where("user_idIN(#{followed_user_ids})ORuser_id=:user_id",
{user_id:user})
end
rspec中的subject,it使用实例
let(:follower){FactoryGirl.create(:user)}
let(:followed){FactoryGirl.create(:user)}
let(:relationship)do
follower.relationships.build(followed_id:followed.id)
end
subject{relationship}
it{shouldbe_valid}
describe"followermethods"do
before{relationship.save}
it{shouldrespond_to(:follower)}
it{shouldrespond_to(:followed)}
its(:follower){should==follower}
its(:followed){should==followed}
end
subject是it后默认的变量
its(:symbol)是从let中取值
css选择器选择页面中的元素
it{shouldhave_selector('h1',text:heading)}
3.rspec如何就行ajax测试
it"shoulddecrementtheRelationshipcount"do
expectdo
xhr:delete,:destroy,id:relationship.id
end.shouldchange(Relationship,:count).by(-1)
end
4.如何写cucumber的测试用例,如下的代码可以参考
Feature:Signingin
Scenario:Unsuccessfulsignin
Givenauservisitsthesigninpage
Whenhesubmitsinvalidsignininformation
Thenheshouldseeanerrormessage
Scenario:Successfulsignin
Givenauservisitsthesigninpage
Andtheuserhasanaccount
Andtheusersubmitsvalidsignininformation
Thenheshouldseehisprofilepage
Andheshouldseeasignoutlink
定义了两个场景,注册成功和注册失败,如下是step文件
Given/^auservisitsthesigninpage$/do
visitsignin_path
end
When/^hesubmitsinvalidsignininformation$/do
click_button"Signin"
end
Then/^heshouldseeanerrormessage$/do
page.shouldhave_selector('div.alert.alert-error')
end
Given/^theuserhasanaccount$/do
@user=User.create(name:"ExampleUser",email:"user@example.com",
password:"foobar",password_confirmation:"foobar")
end
When/^theusersubmitsvalidsignininformation$/do
visitsignin_path
fill_in"Email",with:@user.email
fill_in"Password",with:@user.password
click_button"Signin"
end
Then/^heshouldseehisprofilepage$/do
page.shouldhave_selector('title',text:@user.name)
end
Then/^heshouldseeasignoutlink$/do
page.shouldhave_link('Signout',href:signout_path)
end
https://github.com/chucai/Ruby-on-Rails-Tutorial-by-Michael-Hartl--v3.2-
有比较网站的Rails测试代码,项目比较小,只有三张表,但是代码结构简单,思路清晰,有非常多的值得借鉴和学习的地方。
所有做了这个研究笔记。
1.实现的功能有:
•集成了cucumber和rspec测试
•自己定义的用户认证[没有使用devise]
•简单的微博类型的跟随
•简单的micropost提交
•使用了bootstrap框架
这个小系统所具备的功能有:
1,登录与注册
2,跟随某人
3,个人首页
4,写微博
2.如何在where中使用占用符?
private
#ReturnsanSQLconditionforusersfollowedbythegivenuser.
#Weincludetheuser'sownidaswell.
defself.followed_by(user)
followed_user_ids=%(SELECTfollowed_idFROMrelationships
WHEREfollower_id=:user_id)
where("user_idIN(#{followed_user_ids})ORuser_id=:user_id",
{user_id:user})
end
rspec中的subject,it使用实例
let(:follower){FactoryGirl.create(:user)}
let(:followed){FactoryGirl.create(:user)}
let(:relationship)do
follower.relationships.build(followed_id:followed.id)
end
subject{relationship}
it{shouldbe_valid}
describe"followermethods"do
before{relationship.save}
it{shouldrespond_to(:follower)}
it{shouldrespond_to(:followed)}
its(:follower){should==follower}
its(:followed){should==followed}
end
subject是it后默认的变量
its(:symbol)是从let中取值
css选择器选择页面中的元素
it{shouldhave_selector('h1',text:heading)}
3.rspec如何就行ajax测试
it"shoulddecrementtheRelationshipcount"do
expectdo
xhr:delete,:destroy,id:relationship.id
end.shouldchange(Relationship,:count).by(-1)
end
4.如何写cucumber的测试用例,如下的代码可以参考
Feature:Signingin
Scenario:Unsuccessfulsignin
Givenauservisitsthesigninpage
Whenhesubmitsinvalidsignininformation
Thenheshouldseeanerrormessage
Scenario:Successfulsignin
Givenauservisitsthesigninpage
Andtheuserhasanaccount
Andtheusersubmitsvalidsignininformation
Thenheshouldseehisprofilepage
Andheshouldseeasignoutlink
定义了两个场景,注册成功和注册失败,如下是step文件
Given/^auservisitsthesigninpage$/do
visitsignin_path
end
When/^hesubmitsinvalidsignininformation$/do
click_button"Signin"
end
Then/^heshouldseeanerrormessage$/do
page.shouldhave_selector('div.alert.alert-error')
end
Given/^theuserhasanaccount$/do
@user=User.create(name:"ExampleUser",email:"user@example.com",
password:"foobar",password_confirmation:"foobar")
end
When/^theusersubmitsvalidsignininformation$/do
visitsignin_path
fill_in"Email",with:@user.email
fill_in"Password",with:@user.password
click_button"Signin"
end
Then/^heshouldseehisprofilepage$/do
page.shouldhave_selector('title',text:@user.name)
end
Then/^heshouldseeasignoutlink$/do
page.shouldhave_link('Signout',href:signout_path)
end