2013-04-21 22 views
8

Bu yüzden TDD & için yeni yapıyorum Testlerimde bazı Rspec hataları atıyorum ... Temelde bundle exec rspec spec'i çalıştırdıktan sonra, bazılarında undefined method 'visit' hatası alıyorum benim özellikleri Bu testleri nasıl yapacağınıza dair herhangi bir yardım çok takdir edilecektir: Teşekkürler.Raylar Rspec hatası - undefined method `ziyaret '

Failures: 

1) User pages profile page 
Failure/Error: before { visit user_path(user) } 
NoMethodError: 
    undefined method `visit' for # <RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007ffda8049540> 
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>' 

2) User pages profile page 
Failure/Error: before { visit user_path(user) } 
NoMethodError: 
    undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007ffda4f3ac38> 
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>' 

3) User pages signup page 
Failure/Error: before { visit signup_path } 
NoMethodError: 
    undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_2:0x007ffda8262e58> 
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>' 

4) User pages signup page 
Failure/Error: before { visit signup_path } 
NoMethodError: 
    undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_2:0x007ffda82663c8> 
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>' 

Finished in 9.08 seconds 
24 examples, 4 failures 

Failed examples: 

rspec ./spec/requests/user_pages_spec.rb:11 # User pages profile page 
rspec ./spec/requests/user_pages_spec.rb:12 # User pages profile page 
rspec ./spec/requests/user_pages_spec.rb:18 # User pages signup page 
rspec ./spec/requests/user_pages_spec.rb:19 # User pages signup page 

My Spec/istekleri/user_pages_spec.rb

require 'spec_helper' 

describe "User pages" do 

    subject { page } 

    describe "profile page" do 
    let(:user) { FactoryGirl.create(:user) } # Code to make a user variable 
    before { visit user_path(user) } 

    it { should have_selector('h1', text: user.name) } 
    it { should have_selector('title', text: user.name) } 
    end 

    describe "signup page" do 
    before { visit signup_path } 

    it { should have_selector('h1', text: 'Sign up') } 
    it { should have_selector('title', text: 'Sign up') } 
    end 
end 

Ve Spec/modeller/user_spec.rb son olarak

require 'spec_helper' 

describe User do 

before do 
    @user = User.new(name: "Example User", email: "[email protected]", 
        password: "foobar", password_confirmation: "foobar") 
end 

subject { @user } 

it { should respond_to(:name) } 
it { should respond_to(:email) } 
it { should respond_to(:password_digest) } 
it { should respond_to(:password) } 
it { should respond_to(:password_confirmation) } 
it { should respond_to(:authenticate) } 

it { should be_valid } 

describe "when name is not present" do 
    before { @user.name = " " } 
    it { should_not be_valid } 
end 

describe "when email is not present" do 
    before { @user.email = " " } 
    it { should_not be_valid } 
end 

describe "when name is too long" do 
    before { @user.name = "a" * 51 } 
    it { should_not be_valid } 
end 

describe "when email format is invalid" do 
    it "should be invalid" do 
    addresses = %w[[email protected],com user_at_foo.org [email protected] 
        [email protected]_baz.com [email protected]+baz.com] 
    addresses.each do |invalid_address| 
     @user.email = invalid_address 
     @user.should_not be_valid 
    end  
    end 
end 

describe "when email format is valid" do 
    it "should be valid" do 
    addresses = %w[[email protected] [email protected] [email protected] [email protected]] 
    addresses.each do |valid_address| 
     @user.email = valid_address 
     @user.should be_valid 
    end  
    end 
end 

describe "when email address is already taken" do 
    before do 
    user_with_same_email = @user.dup 
    user_with_same_email.email = @user.email.upcase 
    user_with_same_email.save 
    end 
    it { should_not be_valid } 
end 

describe "when password is not present" do 
    before { @user.password = @user.password_confirmation = " " } 
    it { should_not be_valid } 
end 

describe "with a password that's too short" do 
    before { @user.password = @user.password_confirmation = "a" * 5 } 
    it { should be_invalid } 
end 

describe "return value of authenticate method" do 
    before { @user.save } 
    let(:found_user) { User.find_by_email(@user.email) } 

    describe "with valid password" do 
    it { should == found_user.authenticate(@user.password) } 
    end 

    describe "with invalid password" do 
    let(:user_for_invalid_password) { found_user.authenticate("invalid") } 

    it { should_not == user_for_invalid_password } 
    specify { user_for_invalid_password.should be_false } 
    end 
end 

describe "when password doesn't match confirmation" do 
    before { @user.password_confirmation = "mismatch" } 
    it { should_not be_valid } 
end 

describe "when password confirmation is nil" do 
    before { @user.password_confirmation = nil } 
    it { should_not be_valid } 
end 

end 

Ve

görüşlerim/kullanıcılar/new.html.erb

ve görüntüleme/kullanıcı/show.html.erb

<% provide(:title, @user.name) %> 
<h1><%= @user.name %></h1> 

& benim UsersController

class UsersController < ApplicationController 
    def new 
    end 

    def show 
    @user = User.find(params[:id]) 
    end 
end 
ekledik

Ayrıca sorun burada Billy'nin çözümü

Failures: 

1) User pages signup page 
Failure/Error: it { should have_selector('title', text: 'Sign up') } 
Capybara::ExpectationNotMet: 
    expected to find css "title" with text "Sign up" but there were no matches. Also found "", which matched the selector but not all filters. 
# ./spec/features/user_pages_spec.rb:19:in `block (3 levels) in <top (required)>' 

2) User pages profile page 
Failure/Error: it { should have_selector('title', text: user.name) } 
Capybara::ExpectationNotMet: 
    expected to find css "title" with text "Michael Hartl" but there were no matches. Also found "", which matched the selector but not all filters. 
# ./spec/features/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>' 

Finished in 9.41 seconds 
24 examples, 2 failures 

Failed examples: 

rspec ./spec/features/user_pages_spec.rb:19 # User pages signup page 
rspec ./spec/features/user_pages_spec.rb:12 # User pages profile page 

cevap

22

uyarınca sabitleme sonra şimdi bu yeni hata alıyorum:

./spec/requests/user_pages_spec.rb 

Capybara entegrasyon testlerini requests klasörüne koydunuz. Bu nedenle visit yöntemi işe yaramaz.

sadece spec/requests den spec/features tüm testleri taşımak, düzeltmek için.

+0

, el o klasör oluşturmak gerekir . Altında "spec/features/models" gibi alt klasörler bile oluşturabilirsiniz. –

+1

BB500 @ '/ features' klasörü yalnızca Capybara kullanacak entegrasyon testleri için tasarlanmıştır unutmayın. Model, kontrolör, yardımcılar vb. Gibi diğer fonksiyonel testler için onları hareket ettirmeniz gerekmez ve "ziyaret" gibi Capybara DSL'e ihtiyaç duymazlar. –

+0

Teşekkürler adamım! - Bu test dosyasını yeni özellikler klasörüne taşıdım ve bu testler geçti. (tabiki şimdi yeni bir çiftim var ... belki de burada neler olup bittiğini anlayabilirsiniz?) Mesajı düzenledim. – BB500

21

Eğer Capybara::DSL den visit yöntemini kullanmaya çalışıyorsanız nedeni olur. işaretlerseniz documentation:

 
Capybara is no longer supported in 
request specs as of Capybara 2.0.0. The recommended way to use Capybara is 
with feature specs. 

istek özellikleri için Capybara::DSLspec/features klasöre testleri taşımak veya içermelidir Bu sorunu çözmek için: BB500 @

#spec_helper.rb 
RSpec.configure do |config| 
    #... 
    config.include Capybara::DSL, :type => :request 
+0

Teşekkürler adam - Yardımı takdir et. (Görünüşe göre, yeni bir klasör eklemek yerine Rspec yardım dosyasına – BB500

+0

dahil Capybara'ya "yukarı" cevap vermek için yeterli bir sayıma sahip değilim. – Chookoos

İlgili konular