0

我的 rspec 代码有问题。我写了一些测试。我使用如下语法:主题 {page} 然后我想以这种风格编写测试:它 {should have_content()} 但是当我运行 rspec 它显示错误:

Failure/Error: it{ should have_content("Post associated with #{category.name}") }
     NoMethodError:
       undefined method `it' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x00000007c86ea8>

这是我的所有文件:

require 'spec_helper'

describe "Categories Pages" do
    subject {page}
    let(:user) {FactoryGirl.create(:user)}
    before {sign_in user}
    let(:category) {FactoryGirl.create(:category)}
    let(:p3 )   {FactoryGirl.create(:post, user: user, content: "Foo", title: "Bar", category: category)}


    describe "Categories show page" do 
        before do 
            visit post_path(p3)
            click_link 'Test Category'
        end             

        it "should has elements" do 
            current_path.should == category_path(category)
            it{ should have_content("Post associated with #{category.name}") }
            expect(page).to have_content(p3.content)
            expect(page).to have_link(p3.title, href: post_path(p3))
            expect(page).to have_content(p3.comments.count)
            expect(page).to have_link(p3.category.name, href: category_path(p3.category))
            expect(page).to have_link(p3.user.name, href: user_path(user))
            expect(page).to have_link('All Categories', href: categories_path)
            expect(page).to have_title(full_title('Test Category'))
        end
    end
    describe "Categories index page" do 
        before do 
            visit post_path(p3)
            click_link 'Test Category'
            click_link "All Categories"
        end
        it "should have elements" do
            expect(page).to have_link('Test Category', href: category_path(category))
            expect(page).to have_selector('h1', text: 'All Categories') 
            expect(page).to have_title(full_title('All Categories'))
        end
    end
end

请帮忙。

4

2 回答 2

3

你不能有一个it内在的另一个it

问题出在这一行:

it{ should have_content("Post associated with #{category.name}") }

只需删除it,或将其移到另一个 it 块之外。

page.should have_content("Post associated with #{category.name}")

每个测试有一个断言是个好主意。检查更好的规格

于 2013-08-11T15:51:31.393 回答
1

我不认为你可以嵌套it块。拉这条线

it{ should have_content("Post associated with #{category.name}") }

从周围的it块中取出并使其成为块中的独立测试describe

于 2013-08-11T15:50:20.640 回答