Rails简单的项目研究笔记一

本文介绍了使用 Ruby on Rails 构建小型应用的实战经验,包括集成 cucumber 和 rspec 测试,自定义用户认证,微博类型的跟随功能,以及使用 bootstrap 框架等。详细阐述了如何在 where 中使用占位符,以及在 RSpec 中的 subject 和 it 的使用实例。此外,还提供了关于如何进行 AJAX 测试和撰写 Cucumber 测试用例的指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个项目是
   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(:relationshipdo
    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的测试用例,如下的代码可以参考
   
   FeatureSigning in
  
  ScenarioUnsuccessful signin
    Given a user visits the signin page
    When he submits invalid signin information
    Then he should see an error message
        
  ScenarioSuccessful 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
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值