Rails简单的项目研究笔记一
2012-07-13 16:37
393 查看
这个项目是
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
# Returns an SQL condition for users followed by the given user.
# We include the user's own id as well.
def self.followed_by(user)
followed_user_ids = %(SELECT followed_id FROM relationships
WHERE follower_id = :user_id)
where("user_id IN (#{followed_user_ids}) OR user_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 { should be_valid }
describe "follower methods" do
before { relationship.save }
it { should respond_to(:follower) }
it { should respond_to(:followed) }
its(:follower) { should == follower }
its(:followed) { should == followed }
end
subject 是it 后默认的变量
its(:symbol) 是从 let 中取值
css选择器 选择页面中的元素
it { should have_selector('h1', text: heading) }
3. rspec如何就行ajax测试
it "should decrement the Relationship count" do
expect do
xhr :delete, :destroy, id: relationship.id
end.should change(Relationship, :count).by(-1)
end
4. 如何写cucumber的测试用例,如下的代码可以参考
Feature: Signing in
Scenario: Unsuccessful signin
Given a user visits the signin page
When he submits invalid signin information
Then he should see an error message
Scenario: Successful signin
Given a user visits the signin page
And the user has an account
And the user submits valid signin information
Then he should see his profile page
And he should see a signout link
定义了两个场景, 注册成功和注册失败, 如下是step文件
Given /^a user visits the signin page$/ do
visit signin_path
end
When /^he submits invalid signin information$/ do
click_button "Sign in"
end
Then /^he should see an error message$/ do
page.should have_selector('div.alert.alert-error')
end
Given /^the user has an account$/ do
@user = User.create(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end
When /^the user submits valid signin information$/ do
visit signin_path
fill_in "Email", with: @user.email
fill_in "Password", with: @user.password
click_button "Sign in"
end
Then /^he should see his profile page$/ do
page.should have_selector('title', text: @user.name)
end
Then /^he should see a signout link$/ do
page.should have_link('Sign out', 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
# Returns an SQL condition for users followed by the given user.
# We include the user's own id as well.
def self.followed_by(user)
followed_user_ids = %(SELECT followed_id FROM relationships
WHERE follower_id = :user_id)
where("user_id IN (#{followed_user_ids}) OR user_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 { should be_valid }
describe "follower methods" do
before { relationship.save }
it { should respond_to(:follower) }
it { should respond_to(:followed) }
its(:follower) { should == follower }
its(:followed) { should == followed }
end
subject 是it 后默认的变量
its(:symbol) 是从 let 中取值
css选择器 选择页面中的元素
it { should have_selector('h1', text: heading) }
3. rspec如何就行ajax测试
it "should decrement the Relationship count" do
expect do
xhr :delete, :destroy, id: relationship.id
end.should change(Relationship, :count).by(-1)
end
4. 如何写cucumber的测试用例,如下的代码可以参考
Feature: Signing in
Scenario: Unsuccessful signin
Given a user visits the signin page
When he submits invalid signin information
Then he should see an error message
Scenario: Successful signin
Given a user visits the signin page
And the user has an account
And the user submits valid signin information
Then he should see his profile page
And he should see a signout link
定义了两个场景, 注册成功和注册失败, 如下是step文件
Given /^a user visits the signin page$/ do
visit signin_path
end
When /^he submits invalid signin information$/ do
click_button "Sign in"
end
Then /^he should see an error message$/ do
page.should have_selector('div.alert.alert-error')
end
Given /^the user has an account$/ do
@user = User.create(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end
When /^the user submits valid signin information$/ do
visit signin_path
fill_in "Email", with: @user.email
fill_in "Password", with: @user.password
click_button "Sign in"
end
Then /^he should see his profile page$/ do
page.should have_selector('title', text: @user.name)
end
Then /^he should see a signout link$/ do
page.should have_link('Sign out', href: signout_path)
end
相关文章推荐
- RubyChina源代码研究笔记:Rails3+Mongodb: 如何在Rails项目中整合Mongodb
- Rails一个小项目的研究笔记
- 简单Blog项目笔记之三:用户登录模块(验证码、MD5)
- 利用ruby on rails 快速建立一个简单的项目
- 项目管理---git----快速使用git笔记(一)------git的简单介绍
- 简单OA项目笔记(5):生成、下载excel格式的动态表格(POI)
- 简单Blog项目笔记之九:实现 全选/反选
- go开源cache2go项目蛤蟆笔记——简单使用-
- 简单研究Loader笔记
- 简单CMS项目笔记之三:搜索栏和自定义superAction
- 简单CMS项目笔记之六:信息发布模块
- 简单CMS项目笔记之八:后台信息分类筛选模块
- 数据挖掘笔记-基于项目协同过滤推荐的简单实现
- 项目管理---git----快速使用git笔记(一)------git的简单介绍
- [笔记]--Ruby On Rails创建项目改用Mysql数据库
- CEF3自研究笔记 二、从简单例程开始cefsimple
- Java学习笔记(一)用Eclipse把一个简单游戏项目生成可执行的JAR文件
- 简单Blog项目笔记之六:文章增删
- Java项目开发环境构建工具 Gradle 使用笔记(简单、基本)
- redis集群搭建简单使用 项目中redis单机版+连接池 +集群版的使用 教科书笔记(2)