Code Monkey home page Code Monkey logo

rspec-cells's Introduction

Rspec Cells

Spec your Cells.

<img src=“https://secure.travis-ci.org/apotonick/rspec-cells.png” />

This plugin allows you to test your cells easily using RSpec. Basically, it adds a cells example group with a #render_cell helper.

Cells is Rails’ popular view components framework.

Installation

This gem runs with RSpec2 and Rails >= 3.x, so just add it to your app’s Gemfile.

group :test do
  gem "rspec-cells"
end

Usage

Simply put all your specs in the spec/cells directory. However, let the cell generator do that for you!

rails g cell blog_post show -t rspec

will create an exemplary spec/cells/blog_post_cell_spec.rb for you.

API

In your specs you can use render_cell to assert the rendered markup (or whatever your state is supposed to do). This goes fine with Webrat matchers.

it "renders posts count" do
  render_cell(:posts, :count).should have_selector("p", :content => "4 posts!")
end

You can create a cell instance using the cell method, and then render afterwards. This is helpful when you’re planning to stub things or if you need to pass arguments to the cell constructor.

it "renders empty posts list" do
  posts = cell(:posts)
  posts.stub(:recent_posts).and_return([])

  posts.render_state(:count).should have_selector("p", :content => "No posts!")
end

After preparing the instance you can use render_state for triggering the state.

Capybara

If you want Capybara’s string matchers be sure to bundle at least capybara 0.4.1 in your Gemfile.

group :development, :test do
  gem "capybara", "~> 0.4.1"
end

In order to make the cells test generator work properly, capybara needs to be in both groups.

You can then use capybara’s matchers in your cell spec.

describe PostsCell do
  describe "search posts" do
    let(:search) { render_cell(:posts, :search) }

    it "should have a search field" do
      search.should have_field("Search by Title")
    end

    it "should have a search button" do
      search.should have_button("Search")
    end
  end

  describe "latest posts" do
    subject { render_cell(:posts, :latest) }

    it { should have_css("h3.title", :text => "Latest Posts") }
    it { should have_table("latest_posts") }
    it { should have_link("View all Posts") }
    it { should_not have_button("Create Post") }
    it { should_not have_field("Search by Title") }
  end
end

You can see all capybara matchers and finders here.

Running the specs

Run your examples with

rake spec:cells

If you need more helpers, matchers and stuff, just let us know.

Contributors

LICENSE

Copyright © 2010, Nick Sutterer

Copyright © 2007-2009, Dmytro Shteflyuk <[email protected]> kpumuk.info

Released under the MIT License.

rspec-cells's People

Contributors

apotonick avatar calas avatar docwhat avatar kpumuk avatar hsbt avatar pboling avatar banyan avatar codemonkeysteve avatar alsemyonov avatar felipeik avatar nerian avatar flyerhzm avatar mauidude avatar imomoi avatar

Stargazers

Zigao Wang avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.