未定义的方法'主题'NoMethodError Ruby on Rails,Michael Hartl教程

时间:2013-12-17 12:41:21

标签: ruby-on-rails rspec

目前我一直困扰着一直困扰着我一段时间的错误。任何帮助将不胜感激。

说明:我已经尝试在我的控制台中运行'bundle exec rspec spec /'命令来测试所有脚本是否正常工作但是我遇到了主题为:对象的错误“未定义方法'主题'(NoMethodError )

spec / requests / 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

和spec / models / user_spec.rb

require 'spec_helper'

describe User do
  before do
    @user = User.new(name: "Example User", email: "user@example.com", 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

指定文件'generic.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'

1 个答案:

答案 0 :(得分:1)

您需要在描述块中移动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