2013-12-17 2 views
1

В настоящее время я застрял на ошибке, которая наводила на меня довольно долгое время. Любая помощь будет принята с благодарностью.Неопределенный метод 'subject' NoMethodError Ruby on Rails, учебник Майкла Хартла

Пояснение: Я попытался запустить команду «bundle exec rspec spec /» в моей консоли, чтобы проверить, работают ли все скрипты, однако приветствуется ошибка «неопределенный метод» subject для объекта main: object (NoMethodError)

код для спецификации/запросы/static_pages.rb файл

 require 'spec_helper 

    describe "Static pages" do 

    describe "Home page" do 
     before {visit root_path} 
     it "should have the content 'Sample App'" do 
     expect(page).to have_content('Sample App') 
    end 

    it "should have the title 'Home'" do 
     expect(page).to have_title("Home") 
    end 
    end 

    describe "Help page" do 
     before {visit help_path} 
    it "should have the content 'Help'" do 
     expect(page).to have_content('Help') 
    end 

    it "should have the title 'Help'" do 
     expect(page).to have_title("Help") 
    end 
    end 

    describe "About page" do 
    before {visit about_path} 
    it "should have the content 'About Us'" do 
     expect(page).to have_content('About Us') 
    end 

    it "should have the title 'About Us'" do 
     expect(page).to have_title("About Us") 
    end 
    end 

    describe "Contact Page" do  
    before {visit contact_path} 
    it "should have the content 'Contact'" do 
     expect(page).to have_content('Contact') 
    end 
    it "should have the title 'Contact'" do 
     expect(page).to have_title("Contact") 
    end 
    end 
end 

и спецификации/модели/user_spec.rb

require 'spec_helper' 

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

    subject { @user } 

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


    it { should be_valid } 

    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 eq found_user.authenticate(@user.password) } 
    end 

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

     it { should_not eq user_for_invalid_password } 
     specify { expect(user_for_invalid_password).to be_false } 
    end 
    end 

код для указанного файла «Gener ic.rb»

'subject {page} 

describe "Home page" do 
    before {visit root_path} 
    it {should have_content ('Sample App')} 
    it {should have_title(full_title(''))} 
    it {should_not have_title('| Home')} 
end' 
+2

Привет, могли бы вы вставить сообщение об ошибке вместе с трассировкой? – BroiSatse

+0

Пожалуйста, проявляйте большую осторожность при вставке примеров кода. – phoet

+0

Поскольку вы новичок, я хотел бы указать вам на эту статью: http://nofail.de/2013/10/debugging-rails-applications-in-development/ – phoet

ответ

0

Вы должны переместить subject декларации внутри описание блока:

describe "Home page" do 
    subject {page} 
    before {visit root_path} 
    it {should have_content ('Sample App')} 
    it {should have_title(full_title(''))} 
    it {should_not have_title('| Home')} 
end 
+0

Спасибо всем, кто помог. Это сработало, только для того, чтобы вызвать еще один массив ошибок, ха-ха. Спасибо. – Neal

Смежные вопросы