First. Per * _spec.rb file, should it only exist once? Or is it okay to have multiple scenes in one file?
For example, in spec/request/user_spec.rb:
require'spec_helper'
feature'User actions' do
background do
data = {
:first_name =>'foo',
:last_name =>'bar',
...
}
user = User.new(data, :as => :user)
user.save
end
scenario'User can browse home page 'do
visit root_path
page.should have_content('Homepage')
end
scenario'User should not be able to visit the dashboard' do
visit dashboard_root_path
page.should have_content('You are not authorized to access this page.')
end
end
If there is any problem with the above code structure, Or there is room for improvement. I’m public feedback.
Second. I noticed the code above. If I have config.use_transactional_fixtures=false in spec/spec_helper.rb, it will save the user Twice. This means that in my test database/user table, I will have 2 users named’foo bar’. Is this normal?
Third. I have a form with an HTML button. When the user clicks this button, jQuery will submit the form. How can I test this with Capybara? I don’t think click_button “add” will do this.
Fourth. How do I log in users in Capybara? I am using Devise. sign_in User.first will it do this? Can I access Capybara’s current_user?
Finally, if anyone knows any “getting started” guides/tutorials about Rspec Capybara. Please mention it.
ONE) It’s okay to have multiple scenarios. You can use rspec All the other powerful functions of, so I suggest you also use the context in the underlying code.
TWO) This can be solved by using Rspec Set Gem and database cleaning gems. In addition: The Original Rationale for Set
p>
Warning: Make sure to set the DatabaseCleaner correctly when using set. My own settings (this may be a bit overkill, but it worked for me):
config.before( :suite) do
DatabaseCleaner.clean_with :truncation
end
config.before(:all) do
DatabaseCleaner.clean_with :truncation
end
config.after(:all) do
DatabaseCleaner.clean_with :truncation
end
config.after(:suite) do
DatabaseCleaner. clean_with :truncation
end
3) Yes! click_button “add” should work! The complete capybara API is very useful, but it took me some time to find out. The most important thing is the action and rspec matcher.
Example:
click_button "Add"
page.should have_content("Successfully Added")
You can use the element finder to narrow it down.
Fourth) Devise provides help. There is one sign_in assistant. Read dox :). This is a demo:
feature'User actions' do
background do
data = {
:first_name =>'foo',
:last_name =>'bar',
...
}
@user = User.new(data,: as => :user)
@user.save
end
context "no user is signed in" do
scenario'User can browse home page' do< br /> visit root_path
page.should have_content('Homepage')
end
scenario'User should not be able to visit the dashboard' do
visit dashboard_root_path
page.should have_content('You are not authorized to access this page.')
end
end
context "user is signed in" do
before :each do
sign_in @user
end
[more scenarios]
end
end
Of course, in the end you want to break it down into more specific functions There may be a “public navigation” function for all tests about what visitors see, and then a separate function for user login, etc.
I have some Regarding the capybara question. I might as well ask here, because the RDOC in the github page for Capybara is very suitable for setup and operation. But where is the API or the list of available methods?
First. Per * _spec.rb file, should it only exist once? Or is it okay to have multiple scenes in one file?
For example, in spec/request/user_spec.rb:
require'spec_helper'
feature'User actions' do
background do
data = {
:first_name =>'foo',
:last_name =>'bar',
...
}
user = User.new(data, :as => :user)
user.save
end
scenario'User can browse home page 'do
visit root_path
page.should have_content('Homepage')
end
scenario'User should not be able to visit the dashboard' do
visit dashboard_root_path
page.should have_content('You are not authorized to access this page.')
end
end
If there is any problem with the above code structure, Or there is room for improvement. I’m public feedback.
Second. I noticed the code above. If I have config.use_transactional_fixtures=false in spec/spec_helper.rb, it will save the user Twice. This means that in my test database/user table, I will have 2 users named’foo bar’. Is this normal?
Third. I have a form with an HTML button. When the user clicks this button, jQuery will submit the form. How can I test this with Capybara? I don’t think click_button “add” will do this.
Fourth. How do I log in users in Capybara? I am using Devise. sign_in User.first will it do this? Can I access Capybara’s current_user?
Finally, if anyone knows any “Getting Started” guides/tutorials about Rspec Capybara. Please mention it.
Since I decided not to After I like Cucumber again, I also turned to write a request specification.
ONE) It’s really okay to have multiple scenarios. You can use all the other powerful features of rspec, so I suggest you also use Context in the underlying code.
TWO) This can be solved by using Rspec Set Gem and database cleaning gems. In addition: The Original Rationale for Set
Warning: make sure to use set Set DatabaseCleaner correctly. My own settings (this may be a bit overkill, but it worked for me):
config.before(:suite) do
DatabaseCleaner.clean_with: truncation
end
config.before(:all) do
DatabaseCleaner.clean_with :truncation
end
config.after(:all ) do
DatabaseCleaner.clean_with :truncation
end
config.after(:suite) do
DatabaseCleaner.clean_with :truncation
end
Three) Yes! click_button “add” should work! The complete capybara API is very useful, but it took me some time to find out. The most important thing is the action and rspec matcher.
Example:
click_button "Add"
page.should have_content("Successfully Added")
You can use the element finder to narrow it down.
Fourth) Devise provides help. There is one sign_in assistant. Read dox :). This is a demo:
feature'User actions' do
background do
data = {
:first_name =>'foo',
:last_name =>'bar',
...
}
@user = User.new(data,: as => :user)
@user.save
end
context "no user is signed in" do
scenario'User can browse home page' do< br /> visit root_path
page.should have_content('Homepage')
end
scenario'User should not be able to visit the dashboard' do
visit dashboard_root_path
page.should have_content('You are not authorized to access this page.')
end
end
context "user is signed in" do
before :each do
sign_in @user end
[more scenarios]
end
end
Of course, in the end you want to break it down into more specific functions. There may be A “public navigation” function, used for all tests about what visitors see, and then a separate function for user login, etc.