English 中文(简体)
定义一个步骤,说明数据库的变化
原标题:Cucumber-rails | Defining a step which describe changes in a database

界定描述数据库变化的步骤的最佳做法是什么?

例如,我有以下设想:

Scenario: Creating a user
  Given I am on the users creation page
  When I fill in "Name" with "Chandler Bing"
  And I click "Create User"
  Then the user should be added into the database

我如何更好地界定最后步骤:

Then /^the user should be added into the database$/ do
  User.count.should eq 1
end

或者还有其他一些 way?

感谢。

问题回答

我建议使用Picklegem,其中界定了你的许多数据库步骤,而且这些步骤确实是冰。

我将以两种方式改变这种状况。 第一,网络步骤已用更新的库克文加以解释。 诸如“我以“真主党”填满“真主党”等步骤现在被阻止。 其次,对数据库中的用户数量进行测试,说明全貌。 你们要知道,用户是你创造的用户,意思是你最终添加的同一名称和任何其他特性。 为了处理这些步骤,我发现自己撰写的情景如下:

Scenario: Creating a user
  When I create the following users:
    | name           |
    | Chandler Bing  |
    | Some Other Guy |
  Then I should have the following users:
    | name           |
    | Chandler Bing  |
    | Some Other Guy |

Then in your step definitions, you would do what the web steps were doing:

Given /^I create the following users:$/ do |table|
  table.hashes.each do |row|
    visit new_user_path
    fill_in  Name , with: row[ name ]
    click_button  Create User 
  end
end

Then /^I should have the following users:$/ do |table|
  # Ensure users with same name are taken into account
  table.hashes.size.should == User.count

  table.hashes.each do |row|
    user = User.find_by_name(row[ name ])
    user.should_not be_nil
  end
end

有几个关于这种做法的说明:

  1. It allows your steps to be reusable. If you want to test the negative path and try adding an invalid user you wouldn t have to write a new step.
  2. Less boilerplate code in the feature file. If you have a stakeholder, it should be easier for them to read the steps.
  3. You will actually be learning capybara, which I know I personally took for granted with the web steps.
  4. You can add a bunch of users with much less code.




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签