rspec

Capybara Ambiguity Resolution

瘦欲@ 提交于 2019-12-29 11:31:16
问题 How do I resolve ambiguity in Capybara? For some reason I need links with the same values in a page but I can't create a test since I get the error Failure/Error: click_link("#tag1") Capybara::Ambiguous: Ambiguous match, found 2 elements matching link "#tag1" The reason why I can't avoid this is because of the design. I'm trying to recreate the twitter page with tweets/tags on the right and the tags on the left of the page. Therefore it will be inevitable that identical links page shows up on

How do I generate specs for existing controllers?

本秂侑毒 提交于 2019-12-28 11:50:09
问题 I have several controllers already set up. Now I want to start writing spec tests for them. Is there a command that generates the spec files automatically? I know rails does this for new resources, but I don't know if it does it for existing controllers/models too. 回答1: rails g rspec:controller ControllerName When it asks you to override the existing controller, type n . 回答2: There are two options. If you want an empty spec file, you could try with: rails g rspec:controller ControllerName Now

How can I test the page title with Capybara 2.0?

喜夏-厌秋 提交于 2019-12-27 12:27:19
问题 Trying to test that page contains <title>My Title</title> with: # spec/features/reports_spec.rb require 'spec_helper' feature "Archive Management" do subject { page } describe "Index Page" do before(:all) { 10.times { FactoryGirl.create(:randomreport) } } after(:all) { Report.delete_all } describe "when no search terms present" do before { visit reports_path } it { should have_selector('title', text: 'My Title') } # <= Fails w/Capybara 2.0 it { should have_selector('title') } # <= passes it {

How can I test the page title with Capybara 2.0?

可紊 提交于 2019-12-27 12:22:33
问题 Trying to test that page contains <title>My Title</title> with: # spec/features/reports_spec.rb require 'spec_helper' feature "Archive Management" do subject { page } describe "Index Page" do before(:all) { 10.times { FactoryGirl.create(:randomreport) } } after(:all) { Report.delete_all } describe "when no search terms present" do before { visit reports_path } it { should have_selector('title', text: 'My Title') } # <= Fails w/Capybara 2.0 it { should have_selector('title') } # <= passes it {

`rspec` - comparing two arrays' lengths against each other

浪尽此生 提交于 2019-12-25 18:34:35
问题 Having 2 arrays in index method, I want to test the equality of the arrays' lengths but it's not working (the lengths of the arrays are equal after testing manually) def index @countries = get_countries() @capitals = get_capitals() end Rspec file: describe CountriesController do describe 'index' do it 'countries.length == capitals.length' do expect(assigns(countries.length)).to eq (assigns(capitals.length)) end end end 回答1: Doesn't look like you are making a request to that action... that is.

Why can't I access Rails' URL helpers from my Rspec tests?

一曲冷凌霜 提交于 2019-12-25 13:09:18
问题 Initially I wrote my tests with Cucumber and Capybara, and that worked fine. Then I switched to using Rpsec and Capybara, and that worked fine too. Then I deleted my gemlock file while trying to get the asset pipeline working and suddenly the URL helpers in my tests stopped working. Sample error: Failure/Error: visit report_areas_path(@report) NoMethodError: undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007f978c64e058> # ./spec/features/area_spec.rb:12:in

What is “translate” keyword do in Ruby

蹲街弑〆低调 提交于 2019-12-25 08:22:49
问题 Short question: What is 'translate' word doing and why it's colored as special in my IDE? Long question: I am doing the Odin Project, and code in 04_pig_latin Ruby and RSpec exercise should look like this: def translate(string) # some code end Per the spec which I need to pass: describe "#translate" do it "translates a word beginning with a vowel" do s = translate("apple") expect(s).to eq("appleay") end end In my Cloud9 IDE the word translate is colored blue (like require or render ), so I

rspec Always Returns Errors

我与影子孤独终老i 提交于 2019-12-25 08:22:26
问题 I run: $ bundle exec rspec spec/ And receive this message: C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core.rb:78:in `require': no such file to load -- rspec/expectations (LoadError) from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rspec-core-2.0 .1/lib/rspec/core.rb:78:in `<top (required)>' from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rspec-core-2.0 .1/lib/rspec/autorun.rb:1:in `require' from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems

Testing interaction requests sequences

亡梦爱人 提交于 2019-12-25 07:49:27
问题 There best practices is do not use @instance variables and use let(:object) . But how to write test of call sequences where each step require previous state but not clean state. I'd like to write code like this: describe "intearction" do let(:user1) { ... } let(:user2) { ... } it "request" do get "/api/v1/request", {user2}, token(user1) expect(...).to ... end it "confirm" do get "/api/v1/confirm", {user1}, token(user2) expect(...).to ... end end But that will not work. It may be worked only

RSpec: Authenticating before test with devise_auth_token

社会主义新天地 提交于 2019-12-25 07:49:10
问题 I am using devise_auth_token to authenticate users for an API. I would like to authenticate users before each test is run, but keep getting a 401 error. When I use postman to the endpoint with the correct headers, it works, but fails to work during tests. before(:each) do @user = FactoryGirl.create(:user) end def get_auth headers = @user.create_new_auth_token auth = Hash.new auth["client"] = headers["client"] auth["access-token"] = headers["access-token"] auth["uid"] = headers["uid"] auth[